2013-02-27 189 views
0

我有一个使用Rails 3.1的简单搜索表单,而不是提交按钮,我想要一个放大镜图像,当您点击它时,是一个提交文本内部表单的链接框(text_field_tag)。 Firefox浏览器就是一个例子,它是所有搜索引擎的输入框。我试图用Google搜索如何做到不成功。到目前为止,我有这样的:使用Rails提交文本字段内的链接

= form_tag("/search", :method => "get", :class => "search-forum") do 
    = label_tag "Search for:" 
    = text_field_tag :search 
    = submit_tag "Search", :class => "search-submit" 

什么是最好的方法来实现这一目标?

回答

1

不知道我是否理解好,告诉我,如果它不是你要找的。

不是使用提交按钮,而是通过单击图像或文本字段内的其他元素来执行提交。要acomplish,您可以使用使用jQuery和Ajax unobtrubsive的JavaScript,在这种情况下,你并不需要一个形式:

想这是你的text_field

<input id="search_bar" type="text" placeholder="Search..." name="q"> 
<span class="search-image"></span> 

我们执行搜索HTML所有你需要的是这样的(在一个js文件中):

$(document).ready(function() { 
$(".search-image").click(function(){ 
    var query = $('#search_bar').val() # Get the text on the text field 
    $.get('/route?q=' + query, function(data){ 
    #Perform some actions with the data returned by the server 
    $('#some_container').html(data) 
    }).success(function(){ 
    #Here you can perform other actions if the request is successful 
    alert('successful request') 
    }).error(function{ 
    #In case the request fail 
    alert('request failed') 
    }) 
}); 
} 
+0

那么你如何采取var查询并将其发送回Rails?我有一个用查询搜索的宝石。 – Anoel 2013-02-27 21:17:27

+0

在这种情况下,我保存在'查询'中的文本字段的val在服务器中可以用params [:q]访问 – Alfonso 2013-02-27 21:33:12