Jay Jamison
Jay Jamison

Reputation: 77

ActiveModel::MassAssignmentSecurity::Error: / Can't mass-assign protected attributes: user

I am working through Michael Hartl's Ruby on Rails tutorial, and am modeling users. User_spec.rb test is failing, and my read of the error is that for some reason the attributes aren't being read as accessible, though the code says they should be. I've done rake db:test:prepare as well. Any help would be appreciated greatly.

User model is straightforward.

app/models/user.rb

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

end

Test at spec/models/user_spec.rb is this:

require 'spec_helper'

describe User do

    before { @user = User.new(user: "Example User", email: "[email protected]") }

    subject { @user }

    it { should respond_to(:name) }
    it { should respond_to(:email) }

end

Upvotes: 3

Views: 1573

Answers (2)

Sri
Sri

Reputation: 2273

It should be name not user... Please check the following

require 'spec_helper'

describe User do

  before { @user = User.new(name: "Example User", email: "[email protected]") }

  subject { @user }

  it { should respond_to(:name) }
  it { should respond_to(:email) }
end

You are making mistake while creating a new record with Example User for user. It should be name.

Upvotes: 3

Jason Swett
Jason Swett

Reputation: 45174

Anything you want to update via mass-assignment needs to be in your attr_accessible.

Change this

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

end

to this:

class User < ActiveRecord::Base
  attr_accessible :name, :email, :user

end

Although user doesn't seem like the right attribute name. Is it maybe supposed to be username?

Upvotes: 2

Related Questions