我有一个文本字段:
<%= text_field :search, :class=>'input-xxlarge search-query',:id=>'keyword' %>
然后,单击链接时,我想将此文本字段中的值传递给 Controller 方法。 我不想使用表单和表单提交。
我的链接为:
<a href ="/home/search" >GO</a>
对于这个“搜索”方法,我想传递文本字段值...... 也可以直接进入专为“搜索”操作而设计的页面“/home/search”
我该怎么做??? 谢谢...
最佳答案
在这里阅读link_to send parameters along with the url and grab them on target page
<%= link_to "Go", '/home/search?param1=value' %>
因此,如果您不使用表单,则应该使用 jQuery 将字段值带参数放入属性链接 (href) 中。 jsffidle 上的示例
<%= text_field :search, :class=>'input-xxlarge search-query',:id=>'keyword' %>
<%= link_to "Go", '', :id => "searchlink" %>
$(':input').bind('keypress keydown keyup change',function(){
var word = $(':input[id="keyword"]').val();
$('a[id="searchlink"]').attr("href","/home/search?param1=" + word.toString());
});
在 Controller 中:
if params[:param1] == ""
render :search # or redirect whatever do you want
else
param = params[:param1]
....
end
关于ruby-on-rails - 如何获取text_field Rails的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17468695/