Using Rails link_to for links that post Using Rails link_to for links that post ruby-on-rails ruby-on-rails

Using Rails link_to for links that post


The short answer is that if what you mean by "parameters" is form fields, then you simply can't do this (at least not in a straightforward way that I can see). You should instead use a form with a submit button, styled to look like a link (if that's what you want it to look like).

If on the other hand you had meant query parameters, then this would work:

link_to "Profile", profile_path(@profile.id, param1: 'value1', param2: 'value2'), method: :post


You can encode parameters in the URL this way :

link_to "Profile", 'http://example.com/profile?' + {param1: 'value1', param2: 'value2'}.to_param, method: :post

If it does not fit your needs you are better use a form than a link_to.


Note that if the user has JS disabled or you have removed the unobtrusive JS libraries that come by default, link_to will be silently submitted via a GET request.

In general, I am not very fond of having links that perform POST requests. I think that's the role of a form and a button.

Thus, an easy (and safer) alternative is to use the Rails button_to helper:

button_to 'Profile', profile_path(@profile, param1: 'value1', param2: 'value2')

button_to also supports the method option but as it defaults to post so I've just omitted it.