当db:populate(管理部分)时,教程Michael Hartl在9.4失败

时间:2014-05-18 14:38:26

标签: ruby-on-rails ruby ruby-on-rails-3 ruby-on-rails-4

我没有在我的应用程序上执行管理部分。我刚刚完成了第10章,但现在我想拥有一个管理员用户。

我添加测试,我尝试在控制台中验证:

$ rails console --sandbox
>> user = User.first
>> user.admin?
=> false
>> user.toggle!(:admin)
=> true
>> user.admin?
=> true

没问题,测试是绿色的。

然后我更新了示例数据填充程序,默认情况下让第一个用户成为管理员,但是当我运行时:

$ bundle exec rake db:reset
$ bundle exec rake db:populate
$ bundle exec rake test:prepare

我在db:populate:

中遇到错误
bundle exec rake db:populate
rake aborted!
Can't mass-assign protected attributes: admin
/home/tprails/RubyOnRails/new_app/lib/tasks/sample_data.rake:5:in `block (2 levels) in <top (required)>'
Tasks: TOP => db:populate

所以这是我的 sample_data.rake

namespace :db do
  desc "Fill database with sample data"
  task populate: :environment do

    admin = User.create!(name: "Example User", 
                         email: "example@railstutorial.org",
                         password: "foobar",
                         password_confirmation: "foobar",
                         admin: true)

    99.times do |n|
      name  = Faker::Name.name
      email = "example-#{n+1}@railstutorial.org"
      password  = "password"
      User.create!(name: name,
                   email: email,
                   password: password,
                   password_confirmation: password)
    end
    users = User.all(limit: 6)
    50.times do
      content = Faker::Lorem.sentence(5)
      users.each { |user| user.microposts.create!(content: content) }
    end
  end
end

我的用户模型 user.rb

class User < ActiveRecord::Base

  attr_accessible :email, :name, :password, :password_confirmation
  has_secure_password
  has_many :microposts, dependent: :destroy

  before_save { |user| user.email = 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, :presence => true,
                       :length   => { minimum: 6 }

  validates :password_confirmation, presence: true

  def feed
    Micropost.where("user_id = ?", id)
  end

  private

    def create_remember_token
      self.remember_token = SecureRandom.urlsafe_base64
    end

end

我的迁移: add_admin_to_users.rb

class AddAdminToUsers < ActiveRecord::Migration
  def change
    add_column :users, :admin, :boolean, default: false
  end
end

我希望你理解我的问题并抱歉我的英语不好,

感谢您的帮助


编辑:我修改为:

admin = User.create!(name: "Example User", 
                     email: "example@railstutorial.org",
                     password: "foobar",
                     password_confirmation: "foobar")
admin.toggle!(:admin)

这是正确的解决方案吗?

1 个答案:

答案 0 :(得分:0)

您使用的是哪个Rails版本?

如果Rails 4:您可以删除attr_accessible :email, :name, :password, :password_confirmation,因为您必须在控制器中使用强参数。

如果Rails 3:将:admin添加到attr_accessible :email, :name, :password, :password_confirmation

您收到错误,因为在db population上,您尝试一次性地分配所有值:

admin = User.create!(name: "Example User", email: "example@railstutorial.org", password: "foobar", password_confirmation: "foobar", admin: true)

如果您要逐步添加每个值,则不会出现错误:

admin = User.new
admin.name = "Example User"
...
admin.save

此处有更多信息:Quck Look Strong ParametersGitHub: Strong Parameters