Created
December 2, 2014 20:26
-
-
Save russellquinn/ac55b636a39e1b43694e to your computer and use it in GitHub Desktop.
Adding search to Casein
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
For example, to add search-by-email to a model called User | |
in views/casein/users/index.html.erb | |
<%= content_for :sidebar do %> | |
<li> | |
<%= form_tag casein_users_path, {:method => 'get'} do %> | |
<div class='form-group'> | |
<small><%= label_tag "search", "Search by email", {:class => "control-label"} %></small> | |
<%= text_field_tag "search", params[:search], {:class => "form-control", :size => "100%"} %> | |
</div> | |
<% end %> | |
</li> | |
<% end %> | |
in controllers/casein/users_controller.rb, expand index to be: | |
def index | |
if params[:search] | |
@casein_page_title = "Users (search for: #{params[:search]})" | |
@users = User.where(User.arel_table[:email].matches("%#{params[:search]}%")).order(sort_order("created_at")).paginate :page => params[:page] | |
else | |
@casein_page_title = 'Users' | |
@users = User.order(sort_order(:created_at)).paginate :page => params[:page] | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment