PrimeTimeTran
PrimeTimeTran

Reputation: 2137

self.search body Rails

In Rails I'm trying to create an additional Search field for a Articles project in which the 2nd search field searches the Articles Body and not the Title!

Look at the project here https://glacial-island-18918.herokuapp.com/

index.html.erb

    <div id="main">
     <%= form_tag articles_path, :method => 'get' do %>
      <%= text_field_tag :search, params[:search], class: "form-control", placeholder: "Search Title" %><br><div>
       <%= submit_tag "Search", :name => nil, class: "btn btn-success" %>
        <% end %>

     <%= form_tag articles_path, :method => 'get' do %>
      <%= text_field_tag :body, params[:body], class: "form-control", placeholder: "Search Body" %>
       <%= submit_tag "Search", :name => nil, class: "btn btn-success" %>
        <% end %>

Article.rb

    def self.search(query)
     # where(:title, query) -> This would return an exact match of the query
       where("title like ?", "%#{query}%")
    end

ArticlesController.rb

    def index

     @articles = Article.all
     @markdown = Redcarpet::Markdown.new(Redcarpet::Render::HTML)

    # Adding search feature for Title.
      if params[:search]
         @articles = Article.search(params[:search]).order("created_at DESC")
      else
         @articles = Article.all.order('created_at DESC')
      end
    end

I've searched through these Stack Posts but can't seem to find an answer(I would have posted more but because my reputation is low I can only post two links.)

Search in Rails ==> But it only talks about getting the search to work correctly for the title, no mention of the body

http://railscasts.com/episodes/111-advanced-search-form/ ==> This one sorta had what I was looking for but this seemed a lot more difficult to me, not simply searching for through another column of the row in the table; searching the Body column instead of Title column.

I've tried adding different methods to the Article.rb model, Articles_controller.rb, but I'm just shooting in the dark; any suggestions Stack family?

Your help would be greatly appreciated! Thanks! =)

Upvotes: 0

Views: 1224

Answers (1)

Chris Salzberg
Chris Salzberg

Reputation: 27374

This is not the most elegant solution, but here's an easy way to do it.

First, change your view to use different keys for title and body searches (title_search for title and body_search for body):

<div id="main">
 <%= form_tag articles_path, :method => 'get' do %>
   <%= text_field_tag :title_search, params[:title_search], class: "form-control", placeholder: "Search Title" %><br><div>
   <%= submit_tag "Search", :name => nil, class: "btn btn-success" %>
 <% end %>

 <%= form_tag articles_path, :method => 'get' do %>
   <%= text_field_tag :body_search, params[:body_search], class: "form-control", placeholder: "Search Body" %>
   <%= submit_tag "Search", :name => nil, class: "btn btn-success" %>
 <% end %>
</div>

Then change your controller to

def index
  @articles = Article.all
  @markdown = Redcarpet::Markdown.new(Redcarpet::Render::HTML)

  if (title = params[:title_search].presence) || (summary = params[:body_search].presence)
    @articles = Article.search(title: title, body: body).order("created_at DESC")
  else
    @articles = Article.all.order('created_at DESC')
  end
end

Finally, update your search method to take a hash:

def self.search(query_hash)
   result = all
   result = result.where("title like ?", "%#{query_hash[:title]}%") if query_hash[:title]
   result = result.where("body like ?", "%#{query_hash[:body]}%") if query_hash[:body]
   result
end

Upvotes: 0

Related Questions