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 http://ift.tt/2d0H2pH
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://ift.tt/2d0Knom ==> This one sorta had what I was looking for but this is more about prices and other parameters, not simply searching for through another column of the row; searching Body instead of Title.
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! =)
Aucun commentaire:
Enregistrer un commentaire