0

我已经发现一些人修复了这个问题,修改了 attr_accessible 但我认为这个错误可能出在 gem rake 本身?

rake --trace db:seed

-- 日志输出开始 --

** Invoke db:seed (first_time)
** Execute db:seed
** Invoke db:abort_if_pending_migrations (first_time)
** Invoke environment (first_time)
** Execute environment
** Invoke db:load_config (first_time)
** Execute db:load_config
** Execute db:abort_if_pending_migrations
rake aborted!
Can't mass-assign protected attributes: authentication_token
/Users/devx/.rvm/gems/ruby-1.9.3-p362/gems/activemodel-3.2.8/lib/active_model/mass_assignment_security/sanitizer.rb:48:in `process_removed_attributes'
/Users/devx/.rvm/gems/ruby-1.9.3-p362/gems/activemodel-3.2.8/lib/active_model/mass_assignment_security/sanitizer.rb:20:in `debug_protected_attribute_removal'
/Users/devx/.rvm/gems/ruby-1.9.3-p362/gems/activemodel-3.2.8/lib/active_model/mass_assignment_security/sanitizer.rb:12:in `sanitize'
/Users/devx/.rvm/gems/ruby-1.9.3-p362/gems/activemodel-3.2.8/lib/active_model/mass_assignment_security.rb:230:in `sanitize_for_mass_assignment'
/Users/devx/.rvm/gems/ruby-1.9.3-p362/gems/activerecord-3.2.8/lib/active_record/attribute_assignment.rb:75:in `assign_attributes'
/Users/devx/.rvm/gems/ruby-1.9.3-p362/gems/activerecord-3.2.8/lib/active_record/base.rb:498:in `initialize'
/Users/devx/.rvm/gems/ruby-1.9.3-p362/gems/activerecord-3.2.8/lib/active_record/persistence.rb:44:in `new'
/Users/devx/.rvm/gems/ruby-1.9.3-p362/gems/activerecord-3.2.8/lib/active_record/persistence.rb:44:in `create'
/Users/devx/passbook_rails_example/db/seeds.rb:1:in `<top (required)>'
/Users/devx/.rvm/gems/ruby-1.9.3-p362/gems/activesupport-3.2.8/lib/active_support/dependencies.rb:245:in `load'
/Users/devx/.rvm/gems/ruby-1.9.3-p362/gems/activesupport-3.2.8/lib/active_support/dependencies.rb:245:in `block in load'
/Users/devx/.rvm/gems/ruby-1.9.3-p362/gems/activesupport-3.2.8/lib/active_support/dependencies.rb:236:in `load_dependency'
/Users/devx/.rvm/gems/ruby-1.9.3-p362/gems/activesupport-3.2.8/lib/active_support/dependencies.rb:245:in `load'
/Users/devx/.rvm/gems/ruby-1.9.3-p362/gems/railties-3.2.8/lib/rails/engine.rb:520:in `load_seed'
/Users/devx/.rvm/gems/ruby-1.9.3-p362/gems/activerecord-3.2.8/lib/active_record/railties/databases.rake:309:in `block (2 levels) in <top (required)>'
/Users/devx/.rvm/gems/ruby-1.9.3-p362/gems/rake-0.9.2.2/lib/rake/task.rb:205:in `call'
/Users/devx/.rvm/gems/ruby-1.9.3-p362/gems/rake-0.9.2.2/lib/rake/task.rb:205:in `block in execute'
/Users/devx/.rvm/gems/ruby-1.9.3-p362/gems/rake-0.9.2.2/lib/rake/task.rb:200:in `each'
/Users/devx/.rvm/gems/ruby-1.9.3-p362/gems/rake-0.9.2.2/lib/rake/task.rb:200:in `execute'
/Users/devx/.rvm/gems/ruby-1.9.3-p362/gems/rake-0.9.2.2/lib/rake/task.rb:158:in `block in invoke_with_call_chain'
/Users/devx/.rvm/rubies/ruby-1.9.3-p362/lib/ruby/1.9.1/monitor.rb:211:in `mon_synchronize'
/Users/devx/.rvm/gems/ruby-1.9.3-p362/gems/rake-0.9.2.2/lib/rake/task.rb:151:in `invoke_with_call_chain'
/Users/devx/.rvm/gems/ruby-1.9.3-p362/gems/rake-0.9.2.2/lib/rake/task.rb:144:in `invoke'
/Users/devx/.rvm/gems/ruby-1.9.3-p362/gems/rake-0.9.2.2/lib/rake/application.rb:116:in `invoke_task'
/Users/devx/.rvm/gems/ruby-1.9.3-p362/gems/rake-0.9.2.2/lib/rake/application.rb:94:in `block (2 levels) in top_level'
/Users/devx/.rvm/gems/ruby-1.9.3-p362/gems/rake-0.9.2.2/lib/rake/application.rb:94:in `each'
/Users/devx/.rvm/gems/ruby-1.9.3-p362/gems/rake-0.9.2.2/lib/rake/application.rb:94:in `block in top_level'
/Users/devx/.rvm/gems/ruby-1.9.3-p362/gems/rake-0.9.2.2/lib/rake/application.rb:133:in `standard_exception_handling'
/Users/devx/.rvm/gems/ruby-1.9.3-p362/gems/rake-0.9.2.2/lib/rake/application.rb:88:in `top_level'
/Users/devx/.rvm/gems/ruby-1.9.3-p362/gems/rake-0.9.2.2/lib/rake/application.rb:66:in `block in run'
/Users/devx/.rvm/gems/ruby-1.9.3-p362/gems/rake-0.9.2.2/lib/rake/application.rb:133:in `standard_exception_handling'
/Users/devx/.rvm/gems/ruby-1.9.3-p362/gems/rake-0.9.2.2/lib/rake/application.rb:63:in `run'
/Users/devx/.rvm/gems/ruby-1.9.3-p362/gems/rake-0.9.2.2/bin/rake:33:in `<top (required)>'
/Users/devx/.rvm/gems/ruby-1.9.3-p362/bin/rake:19:in `load'
/Users/devx/.rvm/gems/ruby-1.9.3-p362/bin/rake:19:in `<main>'
/Users/devx/.rvm/gems/ruby-1.9.3-p362/bin/ruby_noexec_wrapper:14:in `eval'
/Users/devx/.rvm/gems/ruby-1.9.3-p362/bin/ruby_noexec_wrapper:14:in `<main>'
Tasks: TOP => db:seed

-- 日志输出结束 --

4

2 回答 2

2

正如您所说,这与 rake 完全无关,这是一个MassAssignment问题,可以通过修改attr_accessible相关模型来修复。

这里实际发生的是尝试创建模型(我猜)seed执行的任务,并且似乎给出的哈希参数包含一个键,它应该如下所示:rakeUserUser.createauthentication_token

User.create name: 'foo', email: 'mail@bar.com', authentication_token: 'qfgsdgf' ...

但是您的模型在其列表User中没有,所以这就是为什么它在尝试创建.:authentication_tokenattr_accessibleUser

如果您在遇到问题时只需要警告而不是异常MassAssignment,则可以更改配置:

在 config/environment/{development,test,production}.rb

config.active_record.mass_assignment_sanitizer = :strict
于 2013-01-12T16:28:23.523 回答
0

只需将 authentication_token 添加到 attr_accessible 例如:我得到的错误

Media1s-Mac-mini:rails_apps media1$ rake clipsfree_import RAILS_ENV=development csvfile=/Users/media1/Desktop/clips/atemp5/demotracks2/import.csv
rake aborted!
Can't mass-assign protected attributes: title
/usr/local/rvm/gems/ruby-1.9.3-p194/gems/activemodel-3.2.11/lib/active_model/mass_assignment_security/sanitizer.rb:48:in `process_removed_attributes'
/usr/local/rvm/gems/ruby-1.9.3-p194/gems/activemodel-3.2.11/lib/active_model/mass_assignment_security/sanitizer.rb:20:in `debug_protected_attribute_removal'
/usr/local/rvm/gems/ruby-1.9.3-p194/gems/activemodel-3.2.11/lib/active_model/mass_assignment_security/sanitizer.rb:12:in `sanitize'
/usr/local/rvm/gems/ruby-1.9.3-p194/gems/activemodel-3.2.11/lib/active_model/mass_assignment_security.rb:230:in `sanitize_for_mass_assignment'
/usr/local/rvm/gems/ruby-1.9.3-p194/gems/activerecord-3.2.11/lib/active_record/attribute_assignment.rb:75:in `assign_attributes'
/usr/local/rvm/gems/ruby-1.9.3-p194/gems/activerecord-3.2.11/lib/active_record/base.rb:497:in `initialize'
/Users/media1/Desktop/rails_apps/lib/tasks/clipsfree_import.rake:14:in `new'
/Users/media1/Desktop/rails_apps/lib/tasks/clipsfree_import.rake:14:in `block (2 levels) in <top (required)>'
/Users/media1/Desktop/rails_apps/lib/tasks/clipsfree_import.rake:5:in `each'
/Users/media1/Desktop/rails_apps/lib/tasks/clipsfree_import.rake:5:in `block in <top (required)>'
Tasks: TOP => clipsfree_import
(See full trace by running task with --trace)

解决方案:在models文件夹的loopsfree.rb中为attr_accessible添加属性title

类 Loopsfree < ActiveRecord::Base attr_accessible :ISRC, :title, :artist, :bpm, :file_name, :genre, :id, :sub_genre end

希望这会有所帮助:) 快乐编码

于 2013-03-09T02:36:13.650 回答