幫助Rails項目快速實現搜索

jopen 12年前發布 | 21K 次閱讀 Rails Ruby開發

本軟件可以幫助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.baiduhome.net/lib/view/home/1355578447416

 本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!