How to set custom parameters on phoenix link helper

I am trying to use the link helper and wanting to add custom parameters. In Rails it is as simple as passing the key value in the helper.

link_to 'Sample link title', articles_path(:param1 => 'value1', :param2 => 'value2')

How can I create custom params on a link helper in phoenix?

iex(5)> h CoolAppUIWeb.Router.Helpers.article_path                                                                

def article_path(conn_or_endpoint, action)                

def article_path(conn_or_endpoint, action, params)            

def article_path(conn_or_endpoint, action, id, params)

iex(6> CoolAppUIWeb.Router.Helpers.article_path(CoolAppUIWeb.Endpoint, :index, what: :do_you, want: :to_do)     

iex(8)>"article list link", to: Helpers.article_path(CoolAppUIWeb.Endpoint, :index, what: :do_you, want: :to_do)) |> Phoenix.HTML.safe_to_string()
"<a href=\"/checkout/users?what=do_you&amp;want=to_do\">article list link</a>"

As you can see, the helpers are usually in the singular form of the resource you’re trying to link to and they take a conn or an endpoint as their first argument. The action will be one of the ones defined in your controller.

What exactly are these extra params for?

Edit: I assume you’ve written it the way it’s used in rails so you’re not confused about the link function, but just to be sure I’ve added a link example using the helper.

1 Like

I am receiving this error when putting a key value

protocol Phoenix.Param not implemented for [custom: :param, another_param: :value2]. This protocol is implemented for: Any, Atom, BitString, Integer, Map

= link "All reviews", to: review_path(@conn, :show, custom: :param, another_param: :value2)

You probably need to use review_path/4 instead of review_path/3

= link "All reviews", to: review_path(@conn, :show, @review, custom: :param, another_param: :value2)

since you probably defined it as get /reviews/:id, ReviewController, :show in your router.

I was doing it incorrectly.

= link "All reviews", to: review_path(@conn, :show, @surfboard, custom: :param, another_param: :value2)

you actually have to specifically pass the id on show instead of using the param protocol.

= link "All reviews", to: review_path(@conn, :show,, custom: :param, another_param: :value2)