1

UPDATE: I am running on Ruby 1.8. The link to the repository is github.com/lauherk/sample_app

I am going through the Ruby on Rails Tutorial by Micheal hartl, and in chapter 9 I am encountering an issue with populating the db with sample users http://ruby.railstutorial.org/chapters/updating-showing-and-deleting-users?version=3.2#sec:sample_users

The code for my rake is:

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

after running both:

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

I get the following error from command line:

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'

(THis specific name changes every time I run the rake)

I have checked and have made sure that in my user model I have the code

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

and have even tried setting

  config.active_record.whitelist_attributes = false

Yet, still have the same outcome at command line.

Any tips to get my database populated with sample users?

Thanks very much.

user1337902
  • 236
  • 3
  • 11
  • I put your code in my rake task and it worked. The "Example User" was created without and error. It seems to have moved on to the loop based on the name in the error. On the other hand, the error says the attribute it is trying to set is "Lawrence Kertzmann" and not 'name' or some other attribute... – EricM Apr 17 '12 at 15:05
  • Are you using Ruby 1.9? You're using new Ruby 1.9 Hash syntax. – retro Apr 17 '12 at 23:03
  • Yea, it's still not working giving the error 'Can't mass-assign protected attributes: Lawrence Kertzmann' – user1337902 Apr 18 '12 at 05:03
  • cloned my repo and converted the code to be compatible with Ruby 1.8.7 and your code from above works. – EricM Apr 18 '12 at 15:35
  • Do you have your code on github? If so, what is the link to it? – EricM Apr 19 '12 at 01:18
  • yes i do, here is a link to the github repo https://github.com/lauherk/sample_app – user1337902 Apr 19 '12 at 07:25

2 Answers2

1

Add

attr_accessible :name

to the model. Currently you only have :user_name (and other values) as accessible.

Larry K
  • 47,808
  • 15
  • 87
  • 140
0

Just add the authentication_token to the attr_accessible Eg: The error that I was getting

Media1s-Mac-mini:rails_apps media1$ rake clipsfree_import RAILS_ENV=development csvfile=/Users/media1/Desktop/clips/atemp5/demotracks2/import.csv rake aborted! Can't mass-assign protected attributes: title /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.3-p194/gems/activerecord-3.2.11/lib/active_record/attribute_assignment.rb:75:in assign_attributes' /usr/local/rvm/gems/ruby-1.9.3-p194/gems/activerecord-3.2.11/lib/active_record/base.rb:497:ininitialize' /Users/media1/Desktop/rails_apps/lib/tasks/clipsfree_import.rake:14:in new' /Users/media1/Desktop/rails_apps/lib/tasks/clipsfree_import.rake:14:inblock (2 levels) in ' /Users/media1/Desktop/rails_apps/lib/tasks/clipsfree_import.rake:5:in each' /Users/media1/Desktop/rails_apps/lib/tasks/clipsfree_import.rake:5:inblock in ' Tasks: TOP => clipsfree_import (See full trace by running task with --trace)

Solution: Added the attribute title to attr_accessible in the loopsfree.rb in the models folder

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

Hope this helps :) Happy coding

Brad Larson
  • 170,088
  • 45
  • 397
  • 571
Pritam Banerjee
  • 17,953
  • 10
  • 93
  • 108