【发布时间】:2013-02-23 22:19:28
【问题描述】:
在我们的客户模型(rails 3.2.12)中,方法find_customers定义如下:
def find_customers
customers = Customerx::Customer.scoped
customers = customers.where("sales_id = ?", sales_id_s) if sales_id_s.present?
customers
end
sales_id_s 通过视图表单通过params[:customer][:sales_id_s] 传入。我们希望使用相同的find_customers 方法在客户控制器索引操作中返回客户。代码是:
def index
if has_index_individual_right?('customerx_customers')
params[:customer][:sales_id_s] = session[:user_id]
customer = Customerx::Customer.new(params[:customer])
@customers = customer.find_customers
else
......
end
......
end
但是代码 params[:customer][:sales_id_s] = session[:user_id] 会导致错误:undefined method[]=' for nil:NilClass`。在调试中,params[:customer] 返回 nil。
有没有办法在索引操作中创建params[:customer] 对象,以便我们可以调用模型方法find_customers?
感谢您的帮助。
【问题讨论】:
标签: ruby-on-rails ruby-on-rails-3 params