Using Metasearch or Ransack with Geocoder

I created a form that supplies the search criteria:

= search_form_for @q do |f| %h3 Search: = f.label :category_id %br = f.collection_select :category_id_eq, Category.all, :id, :name, :include_blank => true %br = f.label "Sub-Category" %br = f.collection_select :subcategory_id_eq, Subcategory.all, :id, :name, :include_blank => true, :prompt => "select category!" %br = f.label "Contains" %br = f.text_field :title_or_details_cont %br = f.submit 

I want to be able to perform searches based on the "Near" functionality of the Rails Geocoder gem. Does anyone know how to include an existing area or specifically use the "Near" area using Meta Search or Ransack?

So far, all my attempts have been futile.

+7
source share
2 answers

This is actually quite simple by simply adding a non-search_form_for form to the form.

In my opinion (note the difference in the two form fields):

 <%= search_form_for @search do |f| %> <%= f.label :will_teach, "Will Teach" %> <%= f.check_box :will_teach %> <%= label_tag :within %> <%= text_field_tag :within, params[:within], {:class => "span1"} %> miles <% end %> 

Then a parameter string is created, such as:

 Parameters: {"utf8"=>"✓", "q"=>{"will_teach"=>"1"}, "within"=>"10", "commit"=>"Search"} 

You can then add some conditional logic to your controller to hide these options and combine the geocoder with Ransack. I check if the parameter is present “inside”, if so, check its number (to_i returns 0 for anything but a number, therefore, check> 0).

Then I combine the “near” geocoder with the Ransack search.

If the parameter “inside” is absent (i.e. the user did not enter a number), I search without geocoding bits.

Finally, I use Kaminari, so this ends with the search results:

  if params[:within].present? && (params[:within].to_i > 0) @search = Tutor.near(request.location.city,params[:within]).search(params[:q]) else @search = Tutor.search(params[:q]) end @tutors = @search.result.page(params[:page]).per(9) 

Hope this helps!

+8
source

there are others who had a similar problem: Integrating the meta_search gem in the index with an existing geocoding gemstone search (rails)

Of course, this is not the same, but perhaps he found a solution for him.

I will look at it tomorrow, since I also need to implement this in my application. I will keep you posted if I find a solution :)

+1
source

All Articles