Rails 4强参数未知密钥

时间:2013-12-27 17:14:37

标签: ruby-on-rails ruby ruby-on-rails-4 strong-parameters

我已经按照Railscast将应用程序迁移到Rails 4并转换为Strong Parameters。 http://railscasts.com/episodes/415-upgrading-to-rails-4

我的创建/新方法正常运行。但是,当我尝试更新记录时,我遇到了问题。我一直收到以下消息:

CustomersController#update中的ArgumentError 未知密钥:first_name

我已经在Stack Overflow上查看了各种帖子,还生成了一个新的Rails 4应用程序,以验证我是否正确地做事。我显然错过了一些东西。

以下是更新方法的控制器代码:

def update
    @customer = Customer.find(customer_params)

    respond_to do |format|
      if @customer.update(customer_params)
        format.html { redirect_to @customer, notice: 'Customer was successfully updated.' }
        format.json { head :ok }
      else
        format.html { render action: "edit" }
        format.json { render json: @customer.errors, status: :unprocessable_entity }
      end
    end
  end

以下是我设置强大参数的地方:

private

def set_customer
      @customer = Customer.find(params[:id])
end

def customer_params
  params.require(:customer).permit(:first_name, :middle_initial, :last_name, :address1, :address2, :city, :state, :zip, :phone, :gender, :email, :membership_id, :date_of_birth)
end

非常感谢任何帮助。

1 个答案:

答案 0 :(得分:5)

您不希望在customer_params声明中使用find,因此您需要使用普通find(params[:id])

强参数仅在创建/更新数据库记录时使用。它们有效地取代了模型中的Rails 3 attr_accessible调用。

相关问题