我在使用 link_to 发送变量时遇到问题。使用 form_for 和提交按钮工作正常,但我需要使用 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 %>
这行不通:(:
<%= 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 %>
最后,link_to 不发送参数,我的 Controller 不接收参数并得到一个错误类型 InvalidFind(调用 Document#find with nil 是无效的):
编辑:
我现在找到了一个解决方案...必须将参数设置到目标中- 参数:
<%= link_to post_path(:post_id => post.id), :method => :post, :remote => true, :html => { :id => "#{@post.id}" }, :url => { :controller => "posts", :action => "follow" } do%>
最佳答案
您发送带有任何 url/path 助手的参数。只需像这样传入散列键/值对即可。
<%= link_to post_path(@post, :my_param => "param value"), .... %>
关于ruby-on-rails - 使用 link_to 方法将变量发送到 Controller rails 3,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9232393/