2010-07-06 66 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。