ransack
Ransack
bundle add ransack
# In the model, whitelist the fields to search
def self.ransackable_attributes(auth_object = nil)
["field1", "field2"]
end
# In the controller
def index
@q = MyModel.ransack(params[:q])
@data = @q.result(distinct: true).page(params[:page])
end
# In the view
<%= search_form_for @q do |f| %>
<%= f.label :name_cont %>
<%= f.search_field :name_cont %>
<%= f.submit "Search" %>
<% end %>
<%#
Do a render 'mymodel/search_form.html.erb'
and put the above in mymodel/_search_form.html.erb
(Remember the underscore in the partial view filename)
In the search_field call, "_cont" is for contains, "_start" is for starts-with
%>
<div>
Sort by: <%= sort_link(@q, :name) do %>Name<% end %>
</div>
ransack.txt · Last modified: by reddy
