帮助Rails项目快速实现搜索
jopen
12年前
本软件可以帮助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"