使用link_to方法rails 3将变量发送到控制器 [英] send variable to controller with link_to method rails 3

查看:73
本文介绍了使用link_to方法rails 3将变量发送到控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对link_to发送变量有问题.使用form_for和Submit按钮可以正常工作,但是我需要使用link_to发送数据.

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.

这对于form_for可以正常工作:

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 %>

这不起作用:(:

<%= 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不发送参数,并且我的控制器未接收到参数并获得错误类型 InvalidFind(使用nil调用Document#find无效):

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):

我现在找到了解决方案...必须将参数设置为目标- 参数:

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%> 

推荐答案

您可以使用任何url/path帮助器发送params.只需像这样传递哈希键/值对.

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"), .... %>

这篇关于使用link_to方法rails 3将变量发送到控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆