[ACCEPTED]-Search in Rails-search

Accepted answer
Score: 45

Although this is not a direct answer to 10 your question, here are some resources that 9 may help you as you are learning about search 8 and getting it implemented in your Rails 7 app.

A Simple search form

Advanced search form

Searching with AJAX

Powerful search functionality with the Sunspot gem

List of the most popular search tools for Ruby

---------------UPDATE----------------

Elasticsearch 6 has been growing in popularity due to some 5 modern awesomeness that it has, such as 4 instant indexing. It has a ruby gem named 3 tire. Definitely worth a look.

Elasticsearch

Tire

---------------UPDATE 2 2----------------

Tire has been retired, and 1 has been replaced by Elasticsearch-ruby

Score: 12

For people coming here and looking for a 5 solution in Rails 4, try the following example:

Posts 4 Controller:

class PostsController < ApplicationController
  def index
    if params[:search]
      @posts = Post.search(params[:search]).order("created_at DESC")
    else
      @posts = Post.all.order('created_at DESC')
    end
  end
end

Post Model:

def self.search(search)
  # Title is for the above case, the OP incorrectly had 'name'
  where("title LIKE ?", "%#{search}%")
end

The index.html.erb 3 remains the same apart from the search form:

<%= form_tag(posts_path, :method => "get", id: "search-form") do %>
  <%= text_field_tag :search, params[:search], placeholder: "Search Posts" %>
  <%= submit_tag "Search" %>
<% end %>

Note 2 that the paths, controllers and columns 1 you are using may be different.

Score: 10

Elasticsearch with gem searchkick

note: searchkick is also 2 contributed by tire) makes you more comfortable 1 to work on.

For more refer:

SearchKick

More Related questions