0

我想列出一个表,其中包含从数据库到表的过滤参数。我为它们分配默认参数,但出现错误:参数数量错误(0 代表 4)

在我的模型中:

class Company < ActiveRecord::Base

scope :filtered, lambda {|count_min, count_max, city, company_status| where(:company_status => company_status, :ap_veh_count => count_min..count_max, :office_adress_city => city, )}

在控制器中:

class LeadsController < ApplicationController

before_filter :confirm_logged_in

def list
        params[:sort] ||= "name"
        params[:direction] ||= "asc"
        params[:company_status] ||= "3" 
        params[:count_min] ||= "8"
        params[:count_max] ||= "20"
        params[:city] ||= "Rīga"
        @companies = Company.filtered.order(params[:sort] + " " + params[:direction])
end

鉴于:

<% @companies.each_with_index do |company, i| %>
<tr>
    <td><%= i + 1 %></td>
    <td><%= company.ap_veh_count  %></td>
    <td><%= link_to company.name, {:action => 'view', :id => company.id} %></td>
    <td><%= company.office_adress_city %></td>
    <td><%= company.phone %></td>
    <td><%= company.company_field %></td>
    <td><%= company.email %></td>
    <td><%= 'taisīt' %></td>
    <td><%= link_to "Atlasīt", {}, :class => 'btn btn-success btn-mini' %></td>

</tr>
<% end %>
4

2 回答 2

3

好吧,您必须将参数传递给您的过滤器方法

@companies = Company.filtered(params[:count_min], params[:count_max], etc.).order...

您可能希望将您的范围定义为:

scope :filtered, lambda { |*args|
  count_min = args.shift || 8
  count_max = args.shift || 20
  city = args.shift || "Rīga"
  company_status = args.shift || 3

  where(
    :count_min = count_min,
    :count_max = count_max,
    :city = city,
    :company_status => company_status
  )
}

为了为您的值设置默认值。但是您仍然必须传递参数(即使它们返回 nil),以便在它们为一组的情况下,它们将覆盖默认值。

于 2012-04-25T13:56:22.973 回答
2
@companies = Company.filtered( !!! )

你应该通过那里count_min, count_max, city, company_status

于 2012-04-25T13:55:52.337 回答