Rails 搜索并向搜索添加过滤器 - 地理编码器

Rails Search and add filters to the search - geocoder


我想要一些关于在我的带有地理编码器的搜索表单上添加过滤器的方法的建议。


我有一个用户模型动物模型(动物有一个类型参数)
join table : Animal_UserAnimal_ID /User_ID)

现在,我可以通过选择 city.That 周围的公里数来定位城市周围的用户。

我想添加动物类型的过滤器,那就太好了:)

搜索如下所示:伦敦 > 10KM > 按类型过滤:狗

如果您对如何做到这一点有所了解,请提前致谢。您会找到搜索 html 和控制器。如果您需要用户模型或动物模型,我也可以 post(它确实是标准的 Has_many / has_many,尽管有关联)

search.html.erb

<div class="search-bar">
<%= form_tag search_path, :class => "webdesigntuts-workshop", method: :get do %>
  <%= label :user, "" %>
  <%= text_field_tag :city, params[:city], placeholder: 'City', :input_html => { :value => 'Paris' } %>

  <%= label :distance, "" %>
  <%= text_field_tag :distance, params[:distance], placeholder: 'km', :input_html => { :value => '10' } %>

  <%= submit_tag "Search" %>
<% end %>
</div>

搜索控制器

class SearchsController < ApplicationController
    def search
    if params[:city].present?
      @searchs = User.near(params[:city], params[:distance] || 10).where("id != ?", current_user.id)
    else
      @searchs = User.all.where("id != ?", current_user.id)
    end
  end

end

试试这个,我基于 Active Record Querying:

User.joins(:animals).where(id:  current_user.id, animals: {type: params[:animal_type]}).near(params[:city], params[:distance] || 10)

这假设您的用户模型 has_many :animals(能够像这样编写 Rails 模型 DSL 真是太棒了)