Send variable to controller using link_to 3 tags

I have a problem with link_to to send a variable. It works fine with the form_for and submit button, but I need to send data using link_to.

This works fine with form_for:

<%= form_for (@post), :method => :post, :remote => true, :html => { :id => "#{@post.id}" }, :url => { :controller => "posts", :action => "follow", :post_id => post.id } do |f| %> <%= f.submit "Follow" %> <% end %> 

This does not work: (:

 <%= link_to post_path(@post), :method => :post, :remote => true, :html => { :id => "#{@post.id}" }, :url => { :controller => "posts", :action => "follow", :post_id => post.id } do%> <em></em> <%= "Follow" %> <% end %> 

This is the last one, link_to does not send parameters, and my controller does not receive parameters and receives the error type InvalidFind (Calling Document # find with nil is invalid):

Edited by:

Now I found a solution ... the parameters should be set to the target, Parameter:

 <%= link_to post_path(:post_id => post.id), :method => :post, :remote => true, :html => { :id => "#{@post.id}" }, :url => { :controller => "posts", :action => "follow" } do%> 
+4
source share
1 answer

You send parameters using any URL / path helpers. Just go to the hash key / value pairs like this.

 <%= link_to post_path(@post, :my_param => "param value"), .... %> 
+7
source

Source: https://habr.com/ru/post/1395812/


All Articles