2010-07-06 64 views
0

我想創建一個非常簡單的搜索部分。它有一個文本框,用於查詢和搜索數據庫。我可以在不使用AJAX或JS的情況下創建remote_function調用嗎?我可以完全保留它「Rails-ee」嗎?Rails按鈕,remote_function。沒有Ajax可能嗎?

<%= text_field_tag "search_term",'', :size => 10 %> 
<%= button "search", :onclick => remote_function(:url => {:action => :fill_in_lots }, 
                :with => "search_term") %> 

回答

0

這不是問題,你需要使用一種叫做正式鏈接的技巧。而不是按鈕,你把一個從提交按鈕。下面是助手的代碼,我用這個:

def formal_link_to(*args, &block) 
    options = html_options = name = nil 
    if block_given? 
     options = args.first 
     html_options = args.second 
     name = capture(&block) 
    else 
     name   = args.first 
     options  = args.second || {} 
     html_options = args.third 
    end 

    method = html_options.delete(:method) || "POST" 
    method = method.to_s.upcase 
    url = url_for(options) 

    html = "<form class=\"formal-link\" action=\"#{url}\" method=\"post\">" 
    html += "<input type=\"hidden\" value=\"#{form_authenticity_token}\" name=\"authenticity_token\" />" 
    html += "<input type=\"hidden\" value=\"#{method}\" name=\"_method\" />" 
    html += link_to(name, "#", html_options) 
    html += "</form>" 

    if block_given? 
     concat(html) 
    else 
     return html 
    end 
    end 

使用此助手像一個正常的link_to,但你可以通過額外的選項:法第二哈希值。例如:

<%= formal_link_to "Fill in lots", { :action => "fill_in_lots" }, { :method => :post } -%> 

備註:1。 當然,這將使整個頁面重新加載,但難免不使用JavaScript。 2.我認爲行動fill_in_lots暴露於POST請求。在GET的情況下,你可以使用普通的link_to helper。