Rails3-如何使用link_to帮助程序将Javascript变量发送到控制器的动作? [英] Rails3 - How to send Javascript Variable to a controller's action with the link_to helper?
本文介绍了Rails3-如何使用link_to帮助程序将Javascript变量发送到控制器的动作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如果我有以下javascript代码
If i have the following javascript code
var myVar = 0;
function setNewValforVar(newValue){
myVar = newValue;
}
我正在调用setNewValforVar
函数n
次
因此,当我单击链接时,它会将myVar
值发送到远程链接之类的控制器动作中,例如
so when I click on a link it'd send myVar
value to the controller action in a remote link like
<%=link_to "My Link",{:action=>"myAction"},:data=>''sval='+myVar',:remote=>true%>
我会使用Rails 2.3.x
I Rails 2.3.x I'd do this with
<%=link_to_remote "My Link",:url=>{:action=>"myAction"},:with=>"'sval='+myVar"%>
而我正在通过以下方式获得控制器的操作
and i was getting this on the controller's action with
params["myVar"]
如何使用link_to
助手在Rails 3上执行此操作?
how do I do this on Rails 3 with the link_to
helper?
推荐答案
Rails 3不再支持此行为.您将必须创建一个表单来提交该值,或者修改链接的href
以在查询字符串中包含该值.
Rails 3 no longer supports this behavior. You will have to either create a form to submit the value, or modify the href
of the link to include the value in the query string.
这篇关于Rails3-如何使用link_to帮助程序将Javascript变量发送到控制器的动作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文