2013-02-27 190 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