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!
Davestephens
source share