使用link_to方法rails 3将变量发送到控制器

hyp*_*jas 4 ruby ruby-on-rails params

我有一个link_to发送变量的问题.使用form_for和submit按钮工作正常,但我需要使用link_to发送数据.

这适用于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 %>
Run Code Online (Sandbox Code Playgroud)

这不起作用:(:

<%= 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 %> 
Run Code Online (Sandbox Code Playgroud)

最后,link_to不发送params,我的控制器没有收到params并得到错误类型InvalidFind(调用文档#find with nil无效):

编辑:

我现在找到了一个解决方案......必须将params设置为target-参数:

<%= link_to post_path(:post_id => post.id), :method => :post, :remote => true, :html => { :id => "#{@post.id}" }, :url => { :controller => "posts", :action => "follow" }  do%> 
Run Code Online (Sandbox Code Playgroud)

小智 8

你发送params与任何网址/路径助手.只需传入这样的哈希键/值对.

<%= link_to post_path(@post, :my_param => "param value"), .... %>
Run Code Online (Sandbox Code Playgroud)