1

我正在关注 Lynda ruby​​ on rails 教程。

这是我必须运行的代码 "subject = Subject.new(:name => "First Subject", :position => 1, :visible => true)"

这是我的错误信息:

ActiveModel::MassAssignmentSecurity::Error: Can't mass-assign protected attributes: name, position, visible
    from /Users/lexi87/.rvm/gems/ruby-1.9.2-p320/gems/activemodel-3.2.9/lib/active_model/mass_assignment_security/sanitizer.rb:48:in `process_removed_attributes'
    from /Users/lexi87/.rvm/gems/ruby-1.9.2-p320/gems/activemodel-3.2.9/lib/active_model/mass_assignment_security/sanitizer.rb:20:in `debug_protected_attribute_removal'
    from /Users/lexi87/.rvm/gems/ruby-1.9.2-p320/gems/activemodel-3.2.9/lib/active_model/mass_assignment_security/sanitizer.rb:12:in `sanitize'
    from /Users/lexi87/.rvm/gems/ruby-1.9.2-p320/gems/activemodel-3.2.9/lib/active_model/mass_assignment_security.rb:230:in `sanitize_for_mass_assignment'
    from /Users/lexi87/.rvm/gems/ruby-1.9.2-p320/gems/activerecord-3.2.9/lib/active_record/attribute_assignment.rb:75:in `assign_attributes'
    from /Users/lexi87/.rvm/gems/ruby-1.9.2-p320/gems/activerecord-3.2.9/lib/active_record/base.rb:497:in `initialize'
    from (irb):9:in `new'
    from (irb):9
    from /Users/lexi87/.rvm/gems/ruby-1.9.2-p320/gems/railties-3.2.9/lib/rails/commands/console.rb:47:in `start'
    from /Users/lexi87/.rvm/gems/ruby-1.9.2-p320/gems/railties-3.2.9/lib/rails/commands/console.rb:8:in `start'
    from /Users/lexi87/.rvm/gems/ruby-1.9.2-p320/gems/railties-3.2.9/lib/rails/commands.rb:41:in `<top (required)>'
    from script/rails:6:in `require'
    from script/rails:6:in `<main>'

这是我来自模型的subject.rb:

class Subject < ActiveRecord::Base
# attr_accessible :title, :body, :name, :position, :visible
end
4

1 回答 1

3

您需要取消注释 attr_accessible,以解释器忽略的 # 字符开头的行

   class Subject < ActiveRecord::Base
        attr_accessible :title, :body, :name, :position, :visible
    end

无法批量分配受保护的属性:名称、位置、可见

attr_accessible指定可以通过批量分配设置的属性列表。

于 2013-01-08T16:54:20.273 回答