【问题标题】:Is it possible to have params[:customer] created in index action (rails 3.2.12)?是否可以在索引操作(rails 3.2.12)中创建 params[:customer]?
【发布时间】: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


    【解决方案1】:

    您可以使用params[:customer] ||= {} 确保它存在(并且是一个哈希)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-02-11
      • 2017-04-24
      • 2018-07-21
      • 2015-09-20
      • 1970-01-01
      • 2017-05-24
      • 2018-12-11
      • 1970-01-01
      相关资源
      最近更新 更多