[RAILS]:使用ransack按地址列出结果

时间:2018-06-12 14:51:24

标签: ruby-on-rails view controller ransack

我是Ror的新人, 我需要一些帮助才能在我的索引页面上显示我的产品。 我使用ransack gem来过滤我的活动结果,但如果在表单中输入,我不能成功地在地理编码地址附近过滤它们。? 每个产品都有一个地址。 求你帮忙!

这是我的代码: 产品/ index.html.erb

<%= search_form_for @search do |f| %>
  <label>Type d'article:</label><br>
  <%= f.label :name_cont, 'article:' %>
  <%= f.search_field :name_cont %>

  <%= f.label :brand_cont, 'marque:' %>
  <%= f.search_field :brand_cont %>

  <%= f.label :size_cont, 'taille:' %>
  <%= f.search_field :size_cont %>

  <label>Prix:</label><br>
  <%= f.label :price_gteq, 'prix min:' %>
  <%= f.number_field :price_gteq %>

  <%= f.label :price_lteq, 'prix max:' %>
  <%= f.number_field :price_lteq %>

  <label>Adresse:</label>
  <%= f.text_field :address, placeholder: "Où est situé votre article ?", id:"user_input_autocomplete_address", class: "form-control" %>
  <input id="street_number" name="street_number" disabled type="hidden">
  <input id="route" name="route" disabled type="hidden">
  <input id="locality" name="locality" disabled type="hidden">
  <input id="country" name="country" disabled type="hidden">

<%= f.submit 'Lancer la recherche', class:'btn btn-info btn-sm' %>                   
<% end %>
...
  <script type="text/javascript"
    src="https://maps.googleapis.com/maps/api/js?libraries=places&key=#{ENV['GOOGLE_API_JAVASCRIPT_KEY']}"></script>

产品controller.rb

class ProductsController < ApplicationController
   before_action :set_product, only: %i[show edit update destroy]
   before_action :authenticate_user!, except: %i[show index slide]

def index
    @search = Product.ransack(params[:q])
    @products = @search.result.where(active: true).near(@address, 50)
  end

private
  def product_params
    params.require(:product).permit(:name, :description, :brand, :category, :color, :size, :state, :price, :address, :status, :active)
  end
end

0 个答案:

没有答案