SkRoR
SkRoR

Reputation: 1199

How to search more than one options on click on submit through ransack search

i want to search both category and location on click on search(Ransack)

my view

<%= 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 %>

This is my view of this form enter image description here my searchcontroller is

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`

command iam getting on executing enter image description here` Here it is searching banglore as category name and location(it should search 'commercial' as category_name(which is under category_id:2) instaed of banglore'

Any help is appreciatable

now i am getting like this, seach query is wrong `enter image description here

Upvotes: 0

Views: 303

Answers (2)

Shani
Shani

Reputation: 2541

not sure what version of ransack you are using but according to documentation ransack, following would fix the problem.

in your view, should use the search_form_for helper

<%= 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 %>

and in your controller

def location
    @q = Property.ransack(params[:q]).result(distinct: true)
end 

Upvotes: 0

Vrushali Pawar
Vrushali Pawar

Reputation: 3803

<%= 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 %>

try this

You are using same parameter name for location as well as for category and which is getting overridden.

Upvotes: 1

Related Questions