Showing posts with label screen. Show all posts
Showing posts with label screen. Show all posts

Thursday, October 10, 2013

To display the searched item in the same page

The following code present in srches_controller

def index   @srches = Srch.all   respond_to do |format|     format.html # index.html.erb     format.json { render :json => @srches }end. ..def search    @srch=Srch.find(:all,                :conditions => ["name LIKE ? OR address LIKE ?", "%#{params[:search]}%", "%#{params[:search]}%"])end

The searched item is now displayed in the new search page.
But I need to display the searched item in the same index page.
What is the solution?

You can pass parameter search to index page & search your results with:

def index  @srches = Srch.scoped  if params[:search].present?    @srches = @srches.where(['name LIKE ? OR address LIKE ?', "%#{params[:search]}%", "%#{params[:search]}%"])  end  respond_to do |format|    format.html    format.json { render :json => @srches }  endend

or you can set scope in your model:

scope :search, lambda{|query|  if query.present?    where(['name LIKE ? OR address LIKE ?', "%#{query}%", "%#{query}%"])  end}

and call it in controller with:

@srches = Srch.search(params[:search])