activerecord-2.3.14打破ruby 1.9.2 :: undefined方法`reject'为“4”:字符串

时间:2011-10-25 17:39:52

标签: ruby-on-rails activerecord ruby-1.9.2

 undefined method `reject' for "4":String 
当我尝试执行@ user.update_attributes(params)时抛出

参数列表如下

"user"=>{"login"=>"admin", "first_name"=>"Admin", "last_name"=>"Admin", "email"=>"nfsurveytest+admin@neurofocus.com", "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/ruby-1.9.2-p290@nf_schedule/gems/activerecord-2.3.14   /lib/active_record/associations.rb:1336:in block in collection_accessor_methods'
/home/narendra/.rvm/gems/ruby-1.9.2-p290@nf_schedule/gems/activerecord-2.3.14/lib/active_record/base.rb:2918:inblock in assign_attributes'
/home/narendra/.rvm/gems/ruby-1.9.2-p290@nf_schedule/gems/activerecord-2.3.14/lib/active_record/base.rb:2914:in each'
/home/narendra/.rvm/gems/ruby-1.9.2-p290@nf_schedule/gems/activerecord-2.3.14/lib/active_record/base.rb:2914:inassign_attributes'
/home/narendra/.rvm/gems/ruby-1.9.2-p290@nf_schedule/gems/activerecord-2.3.14/li b/active_record/base.rb:2787:in attributes='
/home/narendra/.rvm/gems/ruby-1.9.2-p290@nf_schedule/gems/activerecord-2.3.14/lib/active_record/base.rb:2671:inupdate_attributes_inside_transaction'
 /home/narendra/.rvm/gems/ruby-1.9.2-p290@nf_schedule/gems/activerecord-2.3.14/lib/active_record/transactions.rb:229:in block in with_transaction_returning_status'
/home/narendra/.rvm/gems/ruby-1.9.2-p290@nf_schedule/gems/activerecord-2.3.14/lib/active_record/connection_adapters/abstract/database_statements.rb:136:intransaction'
/home/narendra/.rvm/gems/ruby-1.9.2-p290@nf_schedule/gems/activerecord-2.3.14/lib/active_record/transactions.rb:182:in transaction'
/home/narendra/.rvm/gems/ruby-1.9.2-p290@nf_schedule/gems/activerecord-2.3.14/lib/active_record/transactions.rb:228:inwith_transaction_returning_status'
/home/narendra/.rvm/gems/ruby-1.9.2-p290@nf_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'

我认为这打破了ruby 1.9.2中的String不会混合'Enumerable'。 任何人都可以确认这是一个问题还是我错过了什么?

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

1 个答案:

答案 0 :(得分:6)

您正在提供ActiveRecord期待数组的字符串。您可以使用以下内容重新映射控制器中的违规参数:

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

您还可以调整表单,以便该字段的名称为role_ids[],而不仅仅是role_ids,以便将其作为数组提交。