Using Rails link_to for links published

I have a link with which I need to send a sending request. I usually use jQuery and prevent the default link behavior, and then submit the form to the recipient. It seems like Rails should help me. Of course, the link_to method has the ability to specify the POST http method:

 link_to "Profile", 'http://example.com/profile', method: :post 

This works, but I need to add 2 parameters as well. I tried:

 link_to "Profile", 'http://example.com/profile', method: post, param1: 'value1', param2: 'value2' 

It simply added these parameters to the <a> HTML element, but did not send them when the link was clicked:

 <a rel="nofollow" param1="value1" param2="value2" data-method="post" href="http://example.com/profile">Profile</a> 

Is there a way to make a POST request with parameters using link_to or any other Rails method? I am using Rails 3.2.9.

+50
post ruby-on-rails ruby-on-rails-3 link-to
Nov 16 '12 at 10:24
source share
5 answers

The short answer is that if you mean "parameters", these are form fields, then you simply cannot do this (at least not as easy as I see). Instead, you should use a form with a submit button to look like a link (if that's what you want it to look).

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

 link_to "Profile", profile_path(@profile.id, param1: 'value1', param2: 'value2'), method: :post 
+89
Nov 16 '12 at 10:32
source share

You can encode the parameters in the URL like this:

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

If this doesn't suit your needs, you'd better use a form than link_to .

+8
Nov 16 '12 at 10:56
source share

To receive POST data you need a form. However, you do not need a submit button. If you want this to look like a link for some reason, you can make it a link that submits the form via JavaScript. In the example below, the POST resource is simply a REST action that does not require any fields, so there are no form input elements. If you want to post some data, just put the hidden input fields on the form.

 <%= form_tag('http://something_postable', :method => :post, :class => 'internal') %></form> <%= link_to_function('Label for Link', 'previous("form").submit()', :title => 'Hover text for link') %> 

A form is assigned to a class so you can style or hide it using CSS (for example, "display: inline")

+1
Sep 13 '14 at 5:31
source share

The parameters and the http method must be together {param1: 'value1', param2: 'value2', :method: :post}

 <%= link_to "Profile", profile_path(@profile), {param1: 'value1', param2: 'value2', method: :post} %> 
+1
Aug 29 '15 at 8:18
source share

Please note that if the user has disabled JS or you have removed the unobtrusive JS libraries that come by default, link_to will be sent without a request through a GET request.

In general, I do not really like links that execute POST requests. I think the role of form and button.

Thus, a simple (and more secure) option is to use the Rails button_to helper:

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

button_to also supports the method parameter, but by default it is POST , so I just omitted it.

0
Feb 16 '17 at 15:09
source share



All Articles