我跟踪了Railscast将应用程序迁移到Rails 4和向强参数转换的过程。http://railscasts.com/episodes/415-upgrading-to-rails-4
我的Create/New方法运行良好。但是,当我试图更新一张唱片时,我遇到了问题。我一直收到以下信息:
ArgumentError中的CustomersController#update未知密钥: first_name
我在这里查看了Stack溢出的各种帖子,还生成了一个新的Rails 4应用程序,以验证我做的事情是否正确。很明显我漏掉了什么。
以下是update方法的Controller代码:
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任何援助都是非常感谢的。
发布于 2013-12-27 17:22:16
如果不想在customer_params语句中使用find,那么就需要使用普通的find(params[:id])。
强参数仅在创建/更新数据库记录时使用。它们有效地取代了模型中的Rails 3 attr_accessible调用。
https://stackoverflow.com/questions/20804838
复制相似问题