Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

send variable to controller with link_to method rails 3

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%> 
like image 403
hyperrjas Avatar asked Feb 21 '23 12:02

hyperrjas


1 Answers

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"), .... %>
like image 131
Phyo Wai Win Avatar answered Mar 03 '23 06:03

Phyo Wai Win