1

更新:我在 Ruby 1.8 上运行。存储库的链接是 github.com/lauherk/sample_app

我正在阅读 Micheal hartl 的 Ruby on Rails 教程,在第 9 章中,我遇到了用示例用户填充数据库的问题http://ruby.railstutorial.org/chapters/updating-showing-and-deleting-users ?version=3.2#sec:sample_users

我的 rake 的代码是:

namespace :db do
  desc "Fill database with sample data"
  task populate: :environment do
    User.create!(:name => "Example User",
                 :email => "example@railstutorial.org",
                 :password => "foobar",
                 :password_confirmation => "foobar")
    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
  end
end

运行两者后:

bundle exec rake db:reset
bundle exec rake db:populate

我从命令行收到以下错误:

rake aborted!
Can't mass-assign protected attributes: Lawrence Kertzmann
/Library/Ruby/Gems/1.8/gems/activemodel-3.2.2/lib/active_model/mass_assignment_security/sanitizer.rb:48:in `process_removed_attributes'

(每次我运行 rake 时,这个特定的名称都会改变)

我已经检查并确保在我的用户模型中我有代码

attr_accessible :name, :email, :password, :password_confirmation

甚至尝试过设置

  config.active_record.whitelist_attributes = false

然而,在命令行中仍然有相同的结果。

让我的数据库填充示例用户的任何提示?

非常感谢。

4

2 回答 2

1

添加

attr_accessible :name

到模型。目前,您只有:user_name(和其他值)可访问。

于 2012-04-20T04:13:05.207 回答
0

只需将 authentication_token 添加到 attr_accessible 例如:我得到的错误

Media1s-Mac-mini:rails_apps media1$ rake clipsfree_import RAILS_ENV=development csvfile=/Users/media1/Desktop/clips/atemp5/demotracks2/import.csv rake 中止!无法批量分配受保护的属性:标题 /usr/local/rvm/gems/ruby-1.9.3-p194/gems/activemodel-3.2.11/lib/active_model/mass_assignment_security/sanitizer.rb:48:in process_removed_attributes' /usr/local/rvm/gems/ruby-1.9.3-p194/gems/activemodel-3.2.11/lib/active_model/mass_assignment_security/sanitizer.rb:20:indebug_protected_attribute_removal' /usr/local/rvm/gems/ruby- 1.9.3-p194/gems/activemodel-3.2.11/lib/active_model/mass_assignment_security/sanitizer.rb:12:in sanitize' /usr/local/rvm/gems/ruby-1.9.3-p194/gems/activemodel- 3.2.11/lib/active_model/mass_assignment_security.rb:230:insanitize_for_mass_assignment'/usr/local/rvm/gems/ruby-1.9。

解决方案:在models文件夹的loopsfree.rb中为attr_accessible添加属性title

class Loopsfree < ActiveRecord::Base attr_accessible :ISRC, :title, :artist, :bpm, :file_name, :genre, :id, :sub_genre end

希望这会有所帮助:) 快乐编码

于 2013-03-09T02:42:47.453 回答