即使页面在rails 4中加载后,如何保持`params [:search]`值

时间:2014-10-27 13:02:57

标签: ruby-on-rails ruby-on-rails-4

观点:

<%= form_tag :controller => 'hotels',:search=>params[:search] do %>                 
  <%= text_field_tag :search, nil, :class => 'search-box', :required => true,:placeholder=>'Type Your City Name(Bhubaneswar,Cuttack)'%>                 
  <%= submit_tag "Search", :name=>'btnsearch',class: "btn-orange",:method=>'get' %>             
<%end%>

在控制器中:

@hotels= Hotel.where('hotel_location LIKE ?',"%#{params[:search]}%").includes(:offers)

搜索工作正常,但问题是,当我再次加载页面时,它显示数据库中的所有值,过滤器不起作用。

我认为我可以通过将params[:search]传递给网址解决此问题,但我不知道该怎么做。

2 个答案:

答案 0 :(得分:0)

:method => 'get'submit_tag移至form_tag行:

<%= form_tag :controller => 'hotels',:search=>params[:search], method: :get do %> 

答案 1 :(得分:0)

试试这个: -

<%= form_tag :controller => 'hotels', :action => "action_name", method: :get do %>                  
  <%= text_field_tag :search, params[:search], :class => 'search-box', :required => true,:placeholder=>'Type Your City Name(Bhubaneswar,Cuttack)'%>                 
  <%= submit_tag "Search", :name=>'btnsearch',class: "btn-orange",:method=>'get' %>             
<%end%>