Tag: Ransack
gem "ransack"
def index @search = Item.search(params[:q]) @items = @search.result end
<%= search_form_for @search do |f| %> <%= f.text_field :title_cont %> <%= f.submit '検索' %> <% end %> <% @items.each do |item| %> (省略) <% end %>
ernie/ransack_demoと、それを解説した#370 Ransack - RailsCastsが参考になる。
git clone https://github.com/ernie/ransack_demo
bundle install --path vendor/bundle
bundle exec rake db:create:all
bundle exec rake db:migrate
bundle exec rake db:fixtures:load
bundle exec rails s
params[:q][:s] = "rating desc"
@search.result.order("rating desc")
params[:q][:s] = build_sql(params[:sort_order])