幫助Rails項目快速實現搜索
本軟件可以幫助Rails項目快速實現搜索/過濾頁面。具體使用,請參照軟件主頁里的說明。
-
Include gem into Gemfile
gem 'rails-simple-search'
-
Code in model (app/model/search.rb):
class Search < RailsSimpleSearch::Base end
-
Code in controller:
@search = Search.new(User, params[:search]) @search.order = 'email' # optional @users = @search.run
-
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 %>
-
Add route for the post to url “/xxxxxx” (config/route.rb)
match "/xxxxxx" => "yyyyyyy#zzzzzz"
本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!