Reputation:
I'm trying to make a simple search form for my app. All I want to do is search through the internship
column of my user
table. I've tried a few different ways but nothing works for me. I don't really want to use plugins like Solr or Elasticsearch either; I just want to stay within the Rails framework. Does anyone have a working example of code they could post?
Upvotes: 1
Views: 1440
Reputation: 573
Here is mine...
In my Model:
def self.search(search)
if search
losearch = search.downcase
where('lower(artist) LIKE ?, "%#{search.to_s.downcase}%")
end
end
In my Controller:
def index
if params[:search]
@pins = Pin.search(params[:search])
else
@pins = Pin.paginate(:page => params[:page], :per_page => 50).order("created_at DESC")
end
respond_to do |format|
format.html # index.html.erb
format.json { render json: @pins }
# format.js
end
end
In my view:
<div class="index_search">
<%= form_tag(pins_path, :method => "get", class: "navbar-form", id: "search-form") do %>
<div class="input-append">
<%= text_field_tag :search, params[:search], class: "span2 form-control", placeholder: "Search Reviews" %>
<!-- In order to have the "search" icon int the button, we need to use plain HTML instead
of using a Rails form helper -->
<button class="btn btn-default" type="submit"><i class="fa fa-search"></i></button>
<% end %>
</div>
</div>
You'll want to change the "pins_path" above to whichever path you need and the 'lower(artist) LIKE ?' should be 'lower(internship)'
Upvotes: 1