[英]Rails 4 Strong Parameters Unknown Key
我已經按照Railscast的說明將應用程序遷移到Rails 4並過渡到Strong Parameters。 http://railscasts.com/episodes/415-upgrading-to-rails-4
我的Create / New方法工作正常。 但是,當我嘗試更新記錄時遇到問題。 我不斷收到以下消息:
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
非常感謝您的協助。
您不想在find
語句中使用customer_params
,而是要使用常規的find(params[:id])
。
強參數僅在創建/更新數據庫記錄時使用。 它們有效地替換了模型中的Rails 3 attr_accessible
調用。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.