Adding value to live_redirect link

Hi,
I’m getting stuck on how to pass in a value to live_redirect, I have a list of states and want to pass them through as a param on the URL. I’m trying to pass them as ‘item’ but no luck

<%= for item <- ['melbourne', 'sydney', 'brisbane', 'adelaide', 'perth', 'canberra', 'darwin', 'hobart'] do %>
   <%= live_redirect String.capitalize("#{item}") , to: Routes.energy_raters_path(@socket, :index, item) %>
<% end %>```

What happens when you pass it as a keyword?

<%= for item <- ['melbourne', 'sydney', 'brisbane', 'adelaide', 'perth', 'canberra', 'darwin', 'hobart'] do %>
   <%= live_redirect String.capitalize("#{item}") , to: Routes.energy_raters_path(@socket, :index, state: item) %>
<% end %>

You should see ?state=melbourne and you could handle that in the params variable through "state"

Thanks for taking a look.
I gave that a try but I get the error:

Phoenix.Param not implemented for [state: 'melbourne'] of type List. This protocol is implemented for the following type(s): Any, Map, Integer, BitString, Atom

‘melbourne’ is not equal to “melbourne”. It’s a charlist, and I doubt it is what You want…

You might use ~w(melbourne brisbane etc…)

Thanks @kokolegorille yes charlist is not what I wanted, have updated to use double quotes.

I also needed to update my router path to not include /:city and can now see it passing through as ?city=melbourne. Is it possible to pass it as just /melbourne as it’s a nicer pattern?

Yes it’s possible, but You need to customize your routes.

It would be something like this.

# in the Router
live "/energy-rating-path", EnergyRatersLive, :index
live "/energy-rating-path/:city", EnergyRatersLive, :show

# You could call it like so now
<%= for city <- @cities do %>
   <%= live_redirect String.capitalize("#{city}") , to: Routes.energy_raters_path(@socket, :show, city) %>
<% end %>

#In your LiveView
def mount(%{"city" => city}, _session, socket) do
  ...
end
2 Likes

Thanks @tomkonidas that worked perfectly.