我想在点击搜索(Ransack)上搜索类别和位置
我的观点
<%= form_tag location_path, :method=>'get' do %>
<%= select_tag :q, options_from_collection_for_select(Category.all, :id, :name, params[:q]) %>
<%= text_field_tag :q, nil,:placeholder=>"Tell us what you are looking for.." %>
<input type="submit" value="SEARCH" class="btn1 home-search-button" >
<% end %>
def location
q = params[:q]
@key=q
@property = Property.ransack(location_or_category_name_cont: q).result(distinct: true)
end
this search searches only location not category,
on executing i am getting like this,
`url is like this : http://localhost:3000/location?utf8=%E2%9C%93&q=2&q=banglore`
命令iam开始执行 ` 在这里它搜索banglore作为类别名称和位置(它应该搜索商业&#39;作为category_name(在category_id:2下)的banglore&#39;
任何帮助都是值得赞赏的
答案 0 :(得分:1)
<%= form_tag location_path, :method=>'get' do %>
<%= select_tag :category_id, options_from_collection_for_select(Category.all, :id, :name, params[:q]) %>
<%= text_field_tag :q, nil,:placeholder=>"Tell us what you are looking for.." %>
<input type="submit" value="SEARCH" class="btn1 home-search-button" >
<% end %>
试试这个
您正在为位置和类别使用相同的参数名称,并且正在覆盖它。
答案 1 :(得分:0)
不确定您使用的是哪种版本的ransack,但根据文档ransack,以下内容可以解决问题。
在您的视图中,应使用search_form_for帮助
<%= search_form_for @q do |f| %>
<%= f.label :category_name_cont %>
<%= f.select :category_name_cont, options_from_collection_for_select(Category.all, "name", "name", @q.category_name_cont) %>
<%= f.label :location_name_cont %>
<%= f.search_field :location_name_cont %>
<%= f.submit %>
<% end %>
并在您的控制器中
def location
@q = Property.ransack(params[:q]).result(distinct: true)
end