2011-10-25 30 views
2
undefined method `reject' for "4":String 

當我嘗試執行@ user.update_attributes(params)時拋出。activerecord-2.3.14用ruby打破1.9.2 ::未定義的方法`拒絕'爲「4」:字符串

】這個params名單如下

"user"=>{"login"=>"admin", "first_name"=>"Admin", "last_name"=>"Admin", "email"=>"[email protected]", "location_id"=>"1", "last_login_at_text"=>"Never logged in", "password"=>"", "password_confirmation"=>"", "role_ids"=>"4", "active"=>"true", "is_staff"=>"true"}, 

錯誤堆棧如下

/home/narendra/.rvm/gems/[email protected]_schedule/gems/activerecord-2.3.14 /lib/active_record/associations.rb:1336:in block in collection_accessor_methods' 
/home/narendra/.rvm/gems/[email protected]_schedule/gems/activerecord-2.3.14/lib/active_record/base.rb:2918:inblock in assign_attributes' 
/home/narendra/.rvm/gems/[email protected]_schedule/gems/activerecord-2.3.14/lib/active_record/base.rb:2914:in each' 
/home/narendra/.rvm/gems/[email protected]_schedule/gems/activerecord-2.3.14/lib/active_record/base.rb:2914:inassign_attributes' 
/home/narendra/.rvm/gems/[email protected]_schedule/gems/activerecord-2.3.14/li b/active_record/base.rb:2787:in attributes=' 
/home/narendra/.rvm/gems/[email protected]_schedule/gems/activerecord-2.3.14/lib/active_record/base.rb:2671:inupdate_attributes_inside_transaction' 
/home/narendra/.rvm/gems/[email protected]_schedule/gems/activerecord-2.3.14/lib/active_record/transactions.rb:229:in block in with_transaction_returning_status' 
/home/narendra/.rvm/gems/[email protected]_schedule/gems/activerecord-2.3.14/lib/active_record/connection_adapters/abstract/database_statements.rb:136:intransaction' 
/home/narendra/.rvm/gems/[email protected]_schedule/gems/activerecord-2.3.14/lib/active_record/transactions.rb:182:in transaction' 
/home/narendra/.rvm/gems/[email protected]_schedule/gems/activerecord-2.3.14/lib/active_record/transactions.rb:228:inwith_transaction_returning_status' 
/home/narendra/.rvm/gems/[email protected]_schedule/gems/activerecord-2.3.14/lib/active_record/base.rb:2667:in update_attributes' 
/home/narendra/workspace/nf_schedule/app/controllers/users_controller.rb:100:inupdate' 

我認爲這打破了作爲字符串的紅寶石1.9.2不混入「可枚舉」。 任何人都可以確認這是一個問題還是我錯過了什麼?

https://github.com/rails/rails/issues/3434

回答

6

你是哪裏的ActiveRecord的期待陣列提供的字符串。因此,該領域被命名爲role_ids[],而不是僅僅role_ids,這樣,將提交作爲數組

if (params[:user]) 
    params[:user][:role_ids] = [ params[:user][:role_ids] ] 
end 

你也可以調整您的形式:使用這樣的事情,你可以重新映射違規參數在控制器中。

+0

該參數是由表單提交生成的。我希望主動記錄應該優雅地處理這一點。在代碼中無處不在的代碼 – naren

+0

這樣的代碼不會很好。你不應該修補ActiveRecord來處理不良的表單提交。你不應該以這種方式發送結構化數據,所以它首先在它的工作方面令人驚訝。 – tadman

+0

這在Ruby 1.8.7和rails 2.3.14中工作得很好。我在遷移到Ruby 1.9.2時遇到了這個問題。我相信對於我正在使用的單選按鈕,軌道用於處理的方式(所選選項以字符串形式發送)。我不知道是否有幫助將單選按鈕輸入格式化爲數組元素而不是字符串(例如:role_ids「=> [」4「]而不是role_ids」=>「4」)。所以你認爲它仍然是一個糟糕的表單提交或單選按鈕幫手應該修復? – naren

相關問題