ruby-on-rails - 使用 link_to 方法将变量发送到 Controller rails 3

标签 ruby-on-rails ruby parameters

我在使用 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/

相关文章:

mysql - Rails 服务器未在端口 3000 "TCPServer Error: Permission denied - bind (2)"上启动

ruby-on-rails - ElasticSearch 和轮胎/Rails : use two fields for a single facet

ruby - Chef - 从库访问 new_resource

ruby-on-rails - 将 POST 数据从 Controller 提交到 Rails 中的另一个网站

macos - 将多个参数从 Applescript 传递到终端命令脚本

javascript - 为什么在回调中声明一个 var 会取消定义参数? (吊装)

ios - Swift 找不到带有可变参数的 objc 函数

ruby-on-rails - 语法错误,意外 '\n' ,当我运行 rake db :migrate 时,需要 tCOLON2 或 '[' 或 '.'

css - Gruntfile.js SASS 任务配置

ruby-on-rails - Rails 3 iso-3166-country-select 插件如何包含空白并设置优先级国家?