shirawebill

shirawebill

Hi, I am using push_patch with handle_params but I am hitting this error:

def handle_params(%{“page” => page}, _, socket) do
Logger.info(“handle params”)

case page do
  1 -> 
    socket = assign(socket, page: 1)
    {:noreply, push_patch(socket, to: "/portfolio/add-site")}
  2 ->
    socket = assign(socket, page: 2)
    {:noreply, push_patch(socket, to: "/portfolio/add-site")}
end

end

This is what my html page looks like:

<%= if (@page == 2) do %>
<.live_component module={Web.SiteTypeComponent} id=“1” params={@params} changeset={@changeset} page={@page} type={@type} location={@location_input} coords={@coords}/>
<%= else %>
…
<%= live_patch “Continue”, to: Routes.live_path(@socket, Web.AddSiteLive, page: 2), class: “btn-primary-medium” %>

I’m not sure what a I am doing wrong - I want the continue button to load the component for page 2

Thanks! :smiley:

Showing Posts 1 to 6

soup

soup

Please post the actual error message and your router.ex (just the part that includes the route you want to link to).

shirawebill

shirawebill OP

Hi,
Thank you.

I refactored my page a bit and I am no longer getting the error message.
I needed to have 2 handle_params defined, and both are running every time I click “continue”
The issue now is that my page “1” is not being rendered correctly from page “2” - I am missing parts, it’s not loading correctly.

def handle_params(%{“page” => page}, _, socket) do
Logger.info(“handle params 1”)

page = String.to_integer(page)

case page do
  1 -> 
    socket = assign(socket, page: 1)
    {:noreply, push_patch(socket, to: "/portfolio/add-site")}
  2 ->
    socket = assign(socket, page: 2)
    {:noreply, push_patch(socket, to: "/portfolio/add-site")}
end

end

def handle_params(params, _, socket) do
Logger.info(“handle params 2”)
Logger.info(params)
{:noreply, socket}
end

HTML:

<%= if (@page == 2) do %>
<.live_component module={WB.PortfolioWeb.SiteTypeComponent} id=“1” params={@params} changeset={@changeset} page={@page} type={@type} location={@location_input} coords={@coords}/>
<%= else %>
…
<%= live_patch “Continue”, to: Routes.live_path(@socket, WB.PortfolioWeb.AddSiteLive, page: 2), class: “btn-primary-medium” %>
<% end %>

In the component:
<%= live_patch “Site Type & Location”, to: Routes.live_path(@socket, Web.AddSiteLive, page: 1), class: “” %>

Router.ex:
live(“/portfolio/add-site”, AddSiteLive)

soup

soup

The reason you were getting a no match the first time is that you were only matching when params explicitly has the form %{"page" => page}. When you first hit the page, you wont have any query params set, so params will look like this: %{} and Elixir cant find any function that accepts those arguments.

The error message will say something like

 ** (FunctionClauseError) no function clause matching in ForumFixWeb.AddSiteLive.handle_params/3
        lib/forum_fix_web/live/add_site_live.ex:9: ForumFixWeb.AddSiteLive.handle_params(%{}, "http:/....

Which is cluing you into what params were given (the %{}) and what the error is.

and you’ll see this rendered:

Now your second handle_params will always match as a last resort since it effectively has no pattern constraint.

I doubt you want the push_patch, just update the page in assigns and return the updated socket, LiveView will do the rest. This is probably why you’re seeing something run twice.

  def handle_params(%{"page" => page}, _uri, socket) do
    page = String.to_integer(page)

    socket =
      case page do
        1 ->
          assign(socket, page: 1)

        2 ->
          assign(socket, page: 2)
      end
    IO.inspect(page)

    {:noreply, socket}
  end

You might want to re-read the LiveView life cycle stuff in the docs. You may want to refresh yourself on pattern matching, and please post the actual error log next time. Also please try to post with formatting, otherwise it’s hard to follow.

shirawebill

shirawebill OP

Thank you for your help.

I specifically want to use the push patch because I want the input on page 1 to still be there when I go back from the component to page 1, if you understand what I’m saying?

Can you help with that?

benwilson512

benwilson512

Author of Craft GraphQL APIs in Elixir with Absinthe

What should happen if someone removes page from the URL params? It is not possible to guarantee that it is there, users can always remove it. You need to handle a case where it is not present, and set a default if it is not there.

shirawebill

shirawebill OP

That is a good point.
Thank you for your help.

— All posts loaded —

Where Next? Top

Trending in Questions Top

katta
I having some trouble figuring out if I have set myself too strict of standards for my production server. Currently I can handle 75% of r...
New
brecabral
Documentation While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
achenet
Hello, I’m trying to build a basic Phoenix web-app, and I’d like to use Tailwind. However, when I launch mix phx.server, I get an error...
New
kpanic
Hi everyone, I am toying with the idea of building a “match maker” for giving personal help to people that wants to start coding. I sta...
New
asweet-confluent
I recently noticed that Elixir’s Logger defaults its primary log level to :debug when no :logger, :level application configuration is pre...
New
Cxx-mlr
I’m working on a small exercise involving update_in/3, and I came up with this solution: data = %{ name: "Periodic Table", category:...
New
ChrisAmelia
I’ve got trouble wrapping my head around the order in which functions are called in this snippet (from Phoenix’s authentication): toke...
New

Other Trending Topics Top

GenericJam
Edit: 2026 May 15 - This post is archived. Mob is alive!! Main docs: mob v0.7.11 — Documentation A bit of explanation for the slightly c...
New
JesseHerrick
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
garrison
Hobbes is a low-level distributed database for the Elixir programming language. Hobbes provides a simple, safe, and scalable storage lay...
New
mhanberg
Hi everyone! The first release candidate for the Expert language server project is now available! We’ve published a press release detai...
New
budgie
A little off-topic, but I feel like people here have a good head on their shoulders. I used to be quite good at making software. Was luc...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews