I have a problem with link_to to send variable. With form_for and submit button works fine, but I need send the data with 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 not works :(:
<%= 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 last, link_to not send params, and my controller not receive the params and get a error type InvalidFind (Calling Document#find with nil is invalid):
I found now a solution...the params must be set into 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%>
You send params with any url/path helpers. Just pass in hash key/value pairs like this.
<%= link_to post_path(@post, :my_param => "param value"), .... %>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With