Rails - Ransack 不查询

Rails - Ransack doesn't query

我有以下

Sale.rb

belongs_to :client
belongs_to :user

has_many :line_items, dependent: :destroy
has_many :products, through: :line_items
has_many :deposits

accepts_nested_attributes_for :line_items, reject_if: :all_blank, allow_destroy: true

sales_controller.rb

def index
  @q = Sale.ransack(params[:q])
  @sales = @q.result(distinct: true)
end

views/admin/sales/index.html.erb

<%= search_form_for @q, url: admin_sales_path, html: {class: "form-inline"} do |f| %>

  <%= f.search_field :sku, placeholder: "SKU", class: "input perfil" %>
  <%= f.submit 'search', class: "btn btn-primary" %>  

<% end %>

<table class="table">
   <thead>
     <tr>
      <th><%= sort_link @q, :sku, "SKU" %></th>
      <th colspan="3"></th>
     </tr>
   </thead>

  <tbody>
    <% @sales.each do |sale| %>

    ...
    <% end %>
  </tbody>
</table>

最后这是来自终端的日志:

Started GET "/admin/sales?utf8=%E2%9C%93&q%5Bsku%5D=sample&commit=search" for ::1 at 2016-11-24 00:29:33 -0600
Processing by Admin::SalesController#index as HTML
Parameters: {"utf8"=>"✓", "q"=>{"sku"=>"sample"}, "commit"=>"search"}
Sale Load (0.3ms)  SELECT DISTINCT "sales".* FROM "sales"
Rendered admin/sales/index.html.erb within layouts/admin (7.5ms)
Completed 200 OK in 361ms (Views: 346.0ms | ActiveRecord: 1.6ms)

似乎一切正常,但似乎没有任何过滤。

请注意,我对其他模型应用了相同的方法,它们工作正常。

请使用这个

<%= f.search_field :sku_eq, placeholder: "SKU", class: "input perfil" %>

你有点误会

答案很简单: 在 views/admin/sales/index.html.erb

  ...
  <%= f.search_field :sku_eq, placeholder: "SKU", class: "input perfil" %>
  ...

我忘记输入以 f.search_field 结尾的“_eq” :sku_eq