使用元搜索修改基本railscasts Search, AJAX。
class CustomersController < ApplicationController
def index
@search = Customer.search(params[:search])
@customers = @search.all)
end
end
index.html.erb
<%= form_for @search, :html => {:method => :get}, :id => "customers_search", :remote => true do |f| %>
<%= f.label :firstname_or_lastname_contains %>
<%= f.text_field :firstname_or_lastname_contains %><br />
<%= f.submit %>
<% end %>
<div id="customers"> <%= render 'customers' %> ... all the customer basic layout
</div>
<p><%= link_to "New Customer", new_customer_url %></p>
index.js.erb的
$("#customers").html("<%= escape_javascript(render("customers")) %>");
的application.js
$(function() {
$("#customers_search input").keyup(function() {
$.get($("#customers_search").attr("action"), $("#customers_search").serialize(), null, "script");
return false;
});
});
答案 0 :(得分:3)
我不确定你是否能够让它工作,但我能够通过认识到搜索表单默认由meta_search命名为“#customer_search”来实现,所以删除:id =&GT;来自form_for
<强> index.html.erb 强>
<%= form_for @search, :remote => true do |f| %>
<%= f.label :firstname_or_lastname_contains %>
<%= f.text_field :firstname_or_lastname_contains %><br />
<%= f.submit %>
<% end %>
<强>的application.js 强>
$(function() {
$("#customer_search input").keyup(function() {
$.get($("#customer_search").attr("action"), $("#customer_search").serialize(), null, "script");
return false;
});
});
我还没有弄清楚如何从第246集开始以可读的方式呈现文档标题 的 index.js.erb的强>
$("#customers").html("<%= escape_javascript(render("customers")) %>");
document.title = "<%= escape_javascript("#{params[:search].to_s.titleize} - Page #{params[:page] || 1}") %>";
打印
{"Station Name Or Address Contains"=>"As"} - Page 1
到标题栏和历史记录