帮助Rails项目快速实现搜索

jopen 12年前

本软件可以帮助Rails项目快速实现搜索/过滤页面。具体使用,请参照软件主页里的说明。

  1. Include gem into Gemfile

    gem 'rails-simple-search'
  2. Code in model (app/model/search.rb):

    class Search < RailsSimpleSearch::Base  end
  3. Code in controller:

    @search = Search.new(User, params[:search])  @search.order = 'email'  # optional  @users = @search.run
  4. Code in views:

    <% form_for @search, url => "/xxxxxx" do |f| %>      <%=f.label :email %>    <%=f.text_field :email %>      <%=f.label :state%>    <%=f.select "address.state_id", [['AL', 1], ...] %>  <!-- address is an association of model User -->      <%=f.label :post%>    <%=f.text_field "posts.comments.author" %>           <!-- the associations could go even deeper, isn't it POWERFUL? -->      <%=f.submit %>  <% end %>    <% @users.each do |user| %>   <%= # show the attributes of user %>  <% end %>
  5. Add route for the post to url “/xxxxxx” (config/route.rb)

    match "/xxxxxx" => "yyyyyyy#zzzzzz"

项目主页:http://www.open-open.com/lib/view/home/1355578447416