如何通过搜索搜索单击提交时搜索多个选项

时间:2016-03-08 06:17:28

标签: ruby-on-rails ruby-on-rails-3 ransack

我想在点击搜索(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 %>

这是我对此表单的看法 enter image description here 我的搜索控制器是

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开始执行 enter image description here` 在这里它搜索banglore作为类别名称和位置(它应该搜索商业&#39;作为category_name(在category_id:2下)的banglore&#39;

任何帮助都是值得赞赏的

现在我变得像这样,搜索查询是错误的 `enter image description here

2 个答案:

答案 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 
相关问题