4

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):

Edited:

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%> 

1 Answer 1

8

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"), .... %>
Sign up to request clarification or add additional context in comments.

1 Comment

I see that you found the answer yourself too. Would you mind accepting my answer if it's correct? Thanks.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.