1

Lynda ruby​​onrailsチュートリアルをフォローしています。

これが私が実行しなければならないコードです"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 に答える