Simple Search Tutorial

Note: This simple search only works for single words or phrases.

In the Model

1. Add the search method to the model

Open the model and add:

codetitle. app/model/grant.rb

def self.search(search)
  search_condition = "%" + search + "%"
  find(:all, :conditions => ['title LIKE ? OR description LIKE ?', search_condition, search_condition])
end

Here we defined the variable search_condition. In the definition we used wild cards (%) so that our search could more broad. So for example, you could search for penguin and it would return search results that had both penguin and/or penguins. Next we used the built in find method that returns all the results that match our conditions. In the conditions hash we wrote the variables for our search so that it will find grants based on our search in either the title or description of our grants database.

We add this code to the model although it could also be in the controller because the philosophy of programming strives to keep the controller skinny and the model fat.

In the Controller

2. Create the search action in the controller

codetitle. app/controllers/grants_controller.rb

def search
  @grants = Grant.search params[:search]
end

We are calling the method search on the grant model and that’s going to return for us a list of the grants. Our search conditions are in the parameters named search.

In the View
3. Create a partial for the search field. Add to partial:

codetitle. app/views/grants/_search.rhtml

1  <%= form_tag :controller => 'grants', :action => 'search', :method => 'get' do %>
2      <%= text_field_tag :search, params[:search], :id => 'search_field' %>
3      <%= submit_tag "Search", :name => nil %> 
4      <%= link_to_function "Clear", "$('search_field').clear()" %>
5  <% end %> 

Here we are creating a search form that will submit data to the grants controller. Line 1 creates the search form and gets the grants from the search action. Line 2 creates a text field box and the search_field id, which we’ll use later to clear the search box. Line 3 creates a submit tag for the search function. Line 4 creates a function to clear the search box using java script.

4. First, create a simple layout, then add the search function to every page

Open the application.rhtml:

codetitle. app/views/grants/layouts/application.rhtml

  <div id="search">
    <%= render :partial => 'grants/search' %>
  </div>

This allows our users to easily do a search from any page on our web application without having to return to the homepage.

5. Create a partial for the results page

codetitle. app/views/grants/_search_results.rhtml

  <ul>
    <% @grants.each do |grant| %>
  <li><%= link_to grant.title, 
    :action => 'show', :id => grant.id  %></li>
  <% end %>
  </ul>

Here we are creating an unordered list of the search results in a new webpage. This code calls the action show on the search results so that they can nicely display on the page.

More Resources for searches
railscasts.com/episodes/37

 

I gotta favorite this website it seems very helpful . 먹튀검증사이트

 
 

This is my first time i visit here and I found so many interesting stuff in your blog especially it’s discussion, thank you. stainless steel slam latch

 
 

Fabulous post, you have denoted out some fantastic points, I likewise think this s a very wonderful website. I will visit again for more quality contents and also, recommend this site to all. Thanks. 순천렌트카

 
 

Wow, cool post. I’d like to write like this too – taking time and real hard work to make a great article… but I put things off too much and never seem to get started. Thanks though. Nft projects

 
 

A great content material as well as great layout. Your website deserves all of the positive feedback it’s been getting. I will be back soon for further quality contents. 꽁머니

 
 

Hi, I find reading this article a joy. It is extremely helpful and interesting and very much looking forward to reading more of your work.. horizontal rolling discharge chute outlet door

 
 

A totally free tell you which usually they can be a a superb content from the awesome people, we’re very happy to watch this approach. charleston Sunset dinner cruise

 
 

This is very educational content and written well for a change. It’s nice to see that some people still understand how to write a quality post.! 日本百名山

 
 

5 Star Roof Care have been providing Surrey & London quality and competitively priced roofing services for over 30 years. We offer a range of roofs for installation, from lead roofs to Flat, pitched and GRP roofs. Our roofers are fully qualified and trained to give the best possible customer service. Roof repairs Epsom

 
   

You have performed a great job on this article. It’s very precise and highly qualitative. You have even managed to make it readable and easy to read. You have some real writing talent. Thank you so much. ugly cartoon characters