ruby - Cannot chain the searching in rails -
i'm trying searching title/body simple search rails. when tried put matched string in title/body, cannot display expect result. please me have check? thank in advance
article.rb
def self.search(query) # where(:title, query) -> return exact match of query where("title ? or body ?", "%#{query}%", "%#{query}%") end
controller
if params[:search] @articles = article.search(params[:search]) #@articles = article.where(["title || body ?","%#{params[:search]}%"]) else @articles = article.all end
show
<%= form_tag(articles_path, :method => "get", id: "search-form") %> <%= text_field_tag :search, params[:search], placeholder: "title or body" %> <button class="btn btn-success-outline" type="submit">search</button>
please details @ github link : https://github.com/khoinguyen91/coderschool_blog
ok, took quick @ rest of code on github , problem here:
def index #@articles = article.all if params[:search] @articles = article.search(params[:search]) #@articles = article.where(["title || body ?","%#{params[:search]}%"]) else @articles = article.all end @markdown = redcarpet::markdown.new(redcarpet::render::html) if params[:tag] @articles = article.tagged_with(params[:tag]) else @articles = article.all end end
after checking params[:search]
you're checking params[:tag]
blank when submitting search form. means @articles
gets replaced full list of articles (@articles = article.all
).
Comments
Post a Comment