where condition is not working in rails -


in usrshow.html.erb

<%= form_for userview_path, :method => 'get' |f|%>     <%= f.collection_select :city, place.all, :id, :name, :prompt => 'select place' %> <%= text_field_tag :search %> <%= submit_tag "search"%> <% end %> 

in hotels_controller

 def usrshow     if params[:search].present?         @hotels = hotel.where(["city_id = ? , hotels ?",params[:city], "%#{params[:search]}%"])         else         @hotels = hotel.all(:order => 'id desc')     end      respond_to |format|         format.html # index.html.erb         format.json { render :json => @hotels }     end end 

i have search , display hotels based on selected city.but code not working.

you don't need [] brackets in where:

@hotels = hotel.where("city_id = ? , hotels ?",params[:city], "%#{params[:search]}%") 

specify error. not working bad description

you can check sql query in console:

hotel.where("city_id = ? , hotels ?",params[:city], "%#{params[:search]}%").to_sql 

it show exact query. can run db , verify results.


Comments

Popular posts from this blog

javascript - Count length of each class -

What design pattern is this code in Javascript? -

hadoop - Restrict secondarynamenode to be installed and run on any other node in the cluster -