Sco*_* S. 0 ruby ruby-on-rails strong-parameters ruby-on-rails-4
我已经按照Railscast将应用程序迁移到Rails 4并转换为Strong Parameters.http://railscasts.com/episodes/415-upgrading-to-rails-4
我的创建/新方法工作正常.但是,当我尝试更新记录时,我遇到了问题.我一直收到以下消息:
CustomersController中的ArgumentError #update未知密钥:first_name
我在Stack Overflow上查看了各种帖子,还生成了一个新的Rails 4应用程序,以验证我是否正确地做事.我显然错过了一些东西.
这是更新方法的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
Run Code Online (Sandbox Code Playgroud)
这是我设置强大参数的地方:
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
Run Code Online (Sandbox Code Playgroud)
非常感谢任何帮助.
你不想customer_params在你的find陈述中使用,你会想要使用法线find(params[:id]).
强参数仅在创建/更新数据库记录时使用.它们有效地取代了attr_accessible模型中的Rails 3 调用.
| 归档时间: |
|
| 查看次数: |
455 次 |
| 最近记录: |