简体   繁体   English

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

[英]send variable to controller with link_to method rails 3

I have a problem with link_to to send variable. 我有一个link_to发送变量的问题。 With form_for and submit button works fine, but I need send the data with link_to. 使用form_for和submit按钮工作正常,但我需要使用link_to发送数据。

This works fine with form_for: 这适用于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): 最后,link_to不发送params,我的控制器没有收到params并得到错误类型InvalidFind(调用文档#find with nil无效):

Edited: 编辑:

I found now a solution...the params must be set into the target- parameter: 我现在找到了一个解决方案......必须将params设置为target-参数:

<%= 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. 你发送params与任何网址/路径助手。 Just pass in hash key/value pairs like this. 只需传入这样的哈希键/值对。

<%= link_to post_path(@post, :my_param => "param value"), .... %>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM