2014-11-06 97 views
0

我跟隨Railscast第165集(修訂)的發球如何編輯和更新多個記錄在一個窗體中。但是,當我提交表單同時編輯多個記錄,我得到:Rails的ActiveModel :: ForbiddenAttributesError編輯多條記錄

ActiveModel::ForbiddenAttributesError

對於此行:

product.update_attributes(params[:product].reject { |k,v| v.blank? })

PARAMS:

參數:

{"utf8"=>"✓", 
"_method"=>"put", 
"authenticity_token"=>"g5C2siF5GcWfPxhph4utWn8JBs2JXEpIUBDO6OlFyQQ=", 
"product_ids"=>["11142", 
"11143"], 
"product"=>{"user_id"=>"", 
"allow_multi_users"=>"true", 
"state_id"=>"", 
"site"=>"", 
"department"=>"", 
"room"=>"", 
"asset_type_id"=>"", 
"asset_model_id"=>"", 
"sync_with_jss"=>"", 
"carrier_id"=>"", 
"mobile_contract_req_date"=>"", 
"mobile_contract_end_date"=>"", 
"mobile_international_plan"=>"", 
"mobile_tethering"=>"", 
"mobile_account"=>""}, 
"commit"=>"Update"}` 

常模盟友,我想這是因爲我沒有允許強參數的屬性。但這不是一個屬性,它是表達所有價值的參數。

這是我的Products.rb模型,所以它不應該已經接受params [:product]

products_controller.rb

private 
    def product_params 
    params.require(:product).permit(:mobile_account, :mobile_international_plan, :mobile_tethering, :mobile_contract_end_date..) 
    end 

使用Rails 4.0.0

+0

請嘗試在您的問題中添加'puts params [:product]'的值。 – mohameddiaa27 2014-11-06 23:54:33

+0

我在錯誤期間添加了params輸出。 – Devin 2014-11-07 00:09:37

回答

2

試試這個

def update 
    ... 
    product.update_attributes(product_params) 
    ... 
end 

private 

    def product_params 
    params.require(:product).permit(:mobile_account, :mobile_international_plan, :mobile_tethering, :mobile_contract_end_date..) 
    end 

字段列表是您希望您的用戶發送更新或創建領域。

你可以找到更多信息here

錯誤::加載ActiveModel當您嘗試更新與params對象的模型與不被permited,所有出現在表單字段PARAMS ForbiddenAttributesError上升,必須准許更新記錄。

+0

已經有很強的參數設置。添加到問題。 – Devin 2014-11-07 00:06:53

+1

然後你必須調用update_attributes和product_params – 2014-11-07 00:08:31

+0

哇。當然!這非常有意義,我沒有看到它。謝謝。 – Devin 2014-11-07 00:17:21

相關問題