由于'admin'字段,无法使用设计手动创建用户

时间:2013-06-16 12:23:14

标签: ruby-on-rails-3 devise admin

使用Devise,我创建了用户模型,后来我使用迁移向我的模型添加了一个带有default => false:admin 字段。 现在我需要手动创建一个admin用户,所以在rails控制台中我尝试了: superadmin = User.create({ :email => 'email@mail.com', :password => '12345678' , :admin=> true }) 但得到以下错误:

  

ActiveModel :: MassAssignmentSecurity ::错误:无法批量分配受保护的属性:admin       来自/var/lib/gems/1.9.1/gems/activemodel-3.2.13/lib/active_model/mass_assignment_security/sanitizer.rb:48:in process_removed_attributes' from /var/lib/gems/1.9.1/gems/activemodel-3.2.13/lib/active_model/mass_assignment_security/sanitizer.rb:20:in debug_protected_attribute_removal'       来自/var/lib/gems/1.9.1/gems/activemodel-3.2.13/lib/active_model/mass_assignment_security/sanitizer.rb:12:in sanitize' from /var/lib/gems/1.9.1/gems/activemodel-3.2.13/lib/active_model/mass_assignment_security.rb:230:in sanitize_for_mass_assignment'       来自/var/lib/gems/1.9.1/gems/activerecord-3.2.13/lib/active_record/attribute_assignment.rb:75:in assign_attributes' from /var/lib/gems/1.9.1/gems/activerecord-3.2.13/lib/active_record/base.rb:498:in initialize'       来自/var/lib/gems/1.9.1/gems/devise-2.2.4/lib/devise/models/confirmable.rb:46:in initialize' from /var/lib/gems/1.9.1/gems/activerecord-3.2.13/lib/active_record/persistence.rb:44:in new'       来自/var/lib/gems/1.9.1/gems/activerecord-3.2.13/lib/active_record/persistence.rb:44:in create' from (irb):4 from /var/lib/gems/1.9.1/gems/railties-3.2.13/lib/rails/commands/console.rb:47:in start'       来自/var/lib/gems/1.9.1/gems/railties-3.2.13/lib/rails/commands/console.rb:8:in start' from /var/lib/gems/1.9.1/gems/railties-3.2.13/lib/rails/commands.rb:41:in'       来自script / rails:6:require' from script/rails:6:in'

那么还有另一种手动创建用户的方法吗?(使用命令)

1 个答案:

答案 0 :(得分:1)

您尝试为admin分配值,但目前无法分配。要解决此问题,您需要声明admin属性attr_accessible

# app/models/user.rb
class User < ActiveRecord::Base
    attr_accessible :admin
end

这将使您能够以您描绘的精确方式创建新用户。