ActiveModel::MassAssignmentSecurity::Error: Can't mass-assign protected attributes: title
当我attr_accessible :title
在模型中时,我得到了一个。请参阅下面的控制台调用的代码和输出:
用户模型
# == Schema Information
#
# Table name: users
#
# id :integer not null, primary key
# name :string(255)
# email :string(255)
# created_at :datetime not null
# updated_at :datetime not null
# password_digest :string(255)
# remember_token :string(255)
# admin :boolean default(FALSE)
#
class User < ActiveRecord::Base
attr_accessible :name, :email, :password, :password_confirmation
has_secure_password
has_many :cycles, foreign_key: "manager_id"
has_many :reverse_cycles, foreign_key: "employee_id", class_name: "Cycle"
has_many :employees, through: :cycles
has_many :managers, through: :reverse_cycles
has_many :templates, foreign_key: "creator_id"
before_save { self.email.downcase! }
before_save :create_remember_token
validates :name, presence: true, length: { maximum: 50 }
VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i
validates :email, presence: true,
format: { with: VALID_EMAIL_REGEX },
uniqueness: { case_sensitive: false }
validates :password, length: { minimum: 6 }
validates :password_confirmation, presence: true
##### Functions #####
def create_cycle_as_employee!(other_user, date = 1.year.from_now)
rcycle = reverse_cycles.create!(manager_id: other_user.id, ends_on: date)
# self.reload
rcycle
end
def create_cycle_as_manager!(other_user, date = 1.year.from_now)
cycle = cycles.create!(employee_id: other_user.id, ends_on: date)
# self.reload
cycle
end
def create_template!(title)
puts "made it here"
templates.create!(title: title)
end
def is_employee_of?(other_user)
cycles.find_by_manager_id(other_user.id)
end
def is_manager_of?(other_user)
cycles.find_by_employee_id(other_user.id)
end
private
def create_remember_token
self.remember_token = SecureRandom.urlsafe_base64
end
end
模板模型
class Template < ActiveRecord::Base
include PublicID
attr_accessible :title #:creator_id, :deleted, :parent_id, :public_id, :title
belongs_to :creator, class_name: "User"
has_many :questions, class_name: "TemplateQuestion"
validates :title, presence: true
validates :creator_id, presence: true
def create_question!(content, type = 0)
questions.create!(content: content, type: type)
end
end
堆栈跟踪
1.9.3-p362 :004 > user.create_template!("test")
(0.1ms) begin transaction
(0.1ms) rollback transaction
ActiveModel::MassAssignmentSecurity::Error: Can't mass-assign protected attributes: title
from /Users/r/.rvm/gems/ruby-1.9.3-p362@rails3tutorial2ndEd/gems/activemodel-3.2. 11/lib/active_model/mass_assignment_security/sanitizer.rb:48:in `process_removed_attributes'
from /Users/r/.rvm/gems/ruby-1.9.3-p362@rails3tutorial2ndEd/gems/activemodel-3.2. 11/lib/active_model/mass_assignment_security/sanitizer.rb:20:in `debug_protected_attribute_removal'
from /Users/r/.rvm/gems/ruby-1.9.3-p362@rails3tutorial2ndEd/gems/activemodel-3.2. 11/lib/active_model/mass_assignment_security/sanitizer.rb:12:in `sanitize'
from /Users/r/.rvm/gems/ruby-1.9.3-p362@rails3tutorial2ndEd/gems/activemodel-3.2.11/lib/active_model/mass_assignment_security. rb:230:in `sanitize_for_mass_assignment'
from /Users/r/.rvm/gems/ruby-1.9.3-p362@rails3tutorial2ndEd/gems/activerecord-3.2.11/lib/active_record/attribute_assignment. rb:75:in `assign_attributes'
from /Users/r/.rvm/gems/ruby-1.9.3-p362@rails3tutorial2ndEd/gems/activerecord-3.2.11/lib/active_record/base.rb:497:in `initialize'
from /Users/r/.rvm/gems/ruby-1.9.3-p362@rails3tutorial2ndEd/gems/activerecord-3.2.11/lib/active_record/reflection.rb:183:in `new'
from /Users/r/.rvm/gems/ruby-1.9.3-p362@rails3tutorial2ndEd/gems/activerecord-3.2.11/lib/active_record/reflection.rb:183:in `build_association'
from /Users/r/.rvm/gems/ruby-1.9.3-p362@rails3tutorial2ndEd/gems/activerecord-3.2.11/lib/active_record/associations/association. rb:233:in `build_record'
from /Users/r/.rvm/gems/ruby-1.9.3-p362@rails3tutorial2ndEd/gems/activerecord-3.2. 11/lib/active_record/associations/collection_association.rb:434:in `block in create_record'
from /Users/r/.rvm/gems/ruby-1.9.3-p362@rails3tutorial2ndEd/gems/activerecord-3.2. 11/lib/active_record/associations/collection_association.rb:149:in `block in transaction'
from /Users/r/.rvm/gems/ruby-1.9.3-p362@rails3tutorial2ndEd/gems/activerecord-3.2. 11/lib/active_record/connection_adapters/abstract/database_statements.rb:192:in `transaction'
from /Users/r/.rvm/gems/ruby-1.9.3-p362@rails3tutorial2ndEd/gems/activerecord-3.2.11/lib/active_record/transactions.rb:208:in `transaction'
from /Users/r/.rvm/gems/ruby-1.9.3-p362@rails3tutorial2ndEd/gems/activerecord-3.2. 11/lib/active_record/associations/collection_association.rb:148:in `transaction'
from /Users/r/.rvm/gems/ruby-1.9.3-p362@rails3tutorial2ndEd/gems/activerecord-3.2. 11/lib/active_record/associations/collection_association.rb:433:in `create_record'
from /Users/r/.rvm/gems/ruby-1.9.3-p362@rails3tutorial2ndEd/gems/activerecord-3.2. 11/lib/active_record/associations/collection_association.rb:123:in `create!'
from /Users/r/.rvm/gems/ruby-1.9.3-p362@rails3tutorial2ndEd/gems/activerecord-3.2. 11/lib/active_record/associations/collection_proxy.rb:46:in `create!'
from /Users/r/Sites/MicroEval/ror/app/models/user.rb:51:in `create_template!'
from (irb):4
from /Users/r/.rvm/gems/ruby-1.9.3-p362@rails3tutorial2ndEd/gems/railties-3.2.11/lib/rails/commands/console.rb:47:in `start'
from /Users/r/.rvm/gems/ruby-1.9.3-p362@rails3tutorial2ndEd/gems/railties-3.2.11/lib/rails/commands/console.rb:8:in `start'
from /Users/r/.rvm/gems/ruby-1.9.3-p362@rails3tutorial2ndEd/gems/railties-3.2.11/lib/rails/commands.rb:41:in `<top (required)>'
from script/rails:6:in `require'
from script/rails:6:in `<main>'1.9.3-p362 :005 > user.create_template!("test")
这么大的问题是,为什么?我错过了什么吗?我检查了拼写错误,但我假设我要么忽略了某些东西,要么不理解。