我有ROR一個問題:紅寶石在Rails3中:錯誤:無法大規模指派保護的屬性:名稱,位置
我想創造一個名爲「類別2」的模式,我用下面的命令:
rails g model category2
然後,我改變了文件* DB /遷移/ 20120521113409_create_category2s.rb *到:
class CreateCategory2s < ActiveRecord::Migration
def change
create_table :category2s do |t| t.string :name t.integer :position t.timestamps end
add_column :events, :category2_id, :integer
add_index :events, :category2_id
endend
然後,
bundle exec rake db:migrate
當我打開控制檯,輸入
b = Category2.new(:name => 'Ruby', :position => 1)
它返回一個錯誤:
irb(main):009:0> b = Category2.new(:name => 'Ruby', :position => 1)
ActiveModel::MassAssignmentSecurity::Error: Can't mass-assign protected attributes: name, position
from /usr/local/lib/ruby/gems/1.9.1/gems/activemodel-3.2.3/lib/active_model/mass_assignment_security/sanitizer.rb:48:in `process_removed_attributes'
from /usr/local/lib/ruby/gems/1.9.1/gems/activemodel-3.2.3/lib/active_model/mass_assignment_security/sanitizer.rb:20:in `debug_protected_attribute_removal'
from /usr/local/lib/ruby/gems/1.9.1/gems/activemodel-3.2.3/lib/active_model/mass_assignment_security/sanitizer.rb:12:in `sanitize'
from /usr/local/lib/ruby/gems/1.9.1/gems/activemodel-3.2.3/lib/active_model/mass_assignment_security.rb:230:in `sanitize_for_mass_assignment'
from /usr/local/lib/ruby/gems/1.9.1/gems/activerecord-3.2.3/lib/active_record/attribute_assignment.rb:75:in `assign_attributes'
from /usr/local/lib/ruby/gems/1.9.1/gems/activerecord-3.2.3/lib/active_record/base.rb:498:in `initialize'
from (irb):9:in `new'
from (irb):9
from /usr/local/lib/ruby/gems/1.9.1/gems/railties-3.2.3/lib/rails/commands/console.rb:47:in `start'
from /usr/local/lib/ruby/gems/1.9.1/gems/railties-3.2.3/lib/rails/commands/console.rb:8:in `start'
from /usr/local/lib/ruby/gems/1.9.1/gems/railties-3.2.3/lib/rails/commands.rb:41:in `<top (required)>'
from script/rails:6:in `require'
from script/rails:6:in `<main>'
這意味着什麼?
如果我不加參數,我只是喜歡輸入:
irb(main):008:0> a = Category2.new
=> #< Category2 id: nil, name: nil, position: nil, created_at: nil, updated_at: nil>
它看起來不錯!
但爲什麼不能指定的「名稱」和「位置」?
謝謝!
有關於這個主題的確切許多問題。請在提問前進行搜索。這裏有一個例子http://stackoverflow.com/questions/10050797/rails-error-cant-mass-assign-protected-attributes –