hilarl
hilarl

Reputation: 6880

I'm getting an error when i try to add records to my db

i am using Rails 1.9.3 running on Mac OS X Lion. i am trying to add a record to my database and i get the following error:

hisham-agil:twitster hisham$ rails console
Loading development environment (Rails 3.2.5)
1.9.3p194 :001 > User.new(name: 'hilarl', email: '[email protected]', password_digest: 'food', password_confirmation: 'food')
ActiveModel::MassAssignmentSecurity::Error: Can't mass-assign protected attributes: password_digest
    from /usr/local/rvm/gems/ruby-1.9.3-p194/gems/activemodel-3.2.5/lib/active_model/mass_assignment_security/sanitizer.rb:48:in `process_removed_attributes'
    from /usr/local/rvm/gems/ruby-1.9.3-p194/gems/activemodel-3.2.5/lib/active_model/mass_assignment_security/sanitizer.rb:20:in `debug_protected_attribute_removal'
    from /usr/local/rvm/gems/ruby-1.9.3-p194/gems/activemodel-3.2.5/lib/active_model/mass_assignment_security/sanitizer.rb:12:in `sanitize'
    from /usr/local/rvm/gems/ruby-1.9.3-p194/gems/activemodel-3.2.5/lib/active_model/mass_assignment_security.rb:230:in `sanitize_for_mass_assignment'
    from /usr/local/rvm/gems/ruby-1.9.3-p194/gems/activerecord-3.2.5/lib/active_record/attribute_assignment.rb:75:in `assign_attributes'
    from /usr/local/rvm/gems/ruby-1.9.3-p194/gems/activerecord-3.2.5/lib/active_record/base.rb:498:in `initialize'
    from (irb):1:in `new'
    from (irb):1
    from /usr/local/rvm/gems/ruby-1.9.3-p194/gems/railties-3.2.5/lib/rails/commands/console.rb:47:in `start'
    from /usr/local/rvm/gems/ruby-1.9.3-p194/gems/railties-3.2.5/lib/rails/commands/console.rb:8:in `start'
    from /usr/local/rvm/gems/ruby-1.9.3-p194/gems/railties-3.2.5/lib/rails/commands.rb:41:in `<top (required)>'
    from script/rails:6:in `require'
    from script/rails:6:in `<main>'

my User Model:

class User < ActiveRecord::Base
  attr_accessible :email, :name, :password, :password_confirmation


end

enter code here

my database schema:

ActiveRecord::Schema.define(:version => 20120601171139) do

  create_table "users", :force => true do |t|
    t.string   "name"
    t.string   "email"
    t.datetime "created_at",      :null => false
    t.datetime "updated_at",      :null => false
    t.string   "password_digest"
  end

end

i am using the default sqlite3 as a database.

Upvotes: 1

Views: 287

Answers (2)

Anil
Anil

Reputation: 3919

Mass Assignment usually means passing attributes into the call that creates an object as part of an attributes hash.

Try this:

@user = User.new(name: 'hilarl', email: '[email protected]')
@user.password_digest = 'food'
@user.save

Also see:

http://api.rubyonrails.org/classes/ActiveModel/MassAssignmentSecurity/ClassMethods.html

Upvotes: 3

Chris
Chris

Reputation: 12181

password_digest isn't attr_accessible in your user model.

Change the first line to:

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

Upvotes: 0

Related Questions