私たちの顧客モデル(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方法を使用して、customerscontrollerインデックスアクションで顧客を返します。コードは次のとおりです。
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ますか?
助けてくれてありがとう。