2016-11-24 48 views
0

我有如下因素导轨 - 遍寻不查询

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 

的意见/管理/销售/ 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> 

最后这是l og从终端:

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) 

一切似乎工作正常,但似乎没有被过滤。

作为说明我应用这种相同的方法为其他模型,他们工作正常。

+0

<%= f.search_field:sku_eq,占位符:“SKU”,类:“输入perfil”%> 试试吧 –

+0

哇我很惭愧,超级初学者错误,你是对的问题在于: sku_eq(“_eq”结尾)。谢谢。 – user3754535

+0

将其添加到解决方案请accpet –

回答

1

请使用

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

你有一点点的失误

0

答案很简单: 的意见/管理/销售/ index.html.erb

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

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

+0

我想我只是在下面给出了答案,它认为接受作为解决方案而不是添加重复的解决方案是很好的,请点击右边的勾号 –