RailsCasts Pro episodes are now free!

Learn more or hide this

Jason Waldrip's Profile

GitHub User: jwaldrip

Site: jwaldrip.github.com

Comments by Jason Waldrip

Avatar

I am running into the same issue with rails 3.1.3. See below:

error
ruby-1.9.3-p0 :003 > User.new :username => "johnsmith"
ArgumentError: wrong number of arguments (1 for 0)
        from /Volumes/Terra-Nova/jwaldrip/Sites/theirksome/config/initializers/accessible_attributes.rb:6:in `mass_assignment_authorizer'
        from /Volumes/Terra-Nova/jwaldrip/.rvm/gems/ruby-1.9.3-p0/gems/activemodel-3.1.3/lib/active_model/mass_assignment_security.rb:209:in `sanitize_for_mass_assignment'
        from /Volumes/Terra-Nova/jwaldrip/.rvm/gems/ruby-1.9.3-p0/gems/activerecord-3.1.3/lib/active_record/base.rb:1744:in `assign_attributes'
        from /Volumes/Terra-Nova/jwaldrip/.rvm/gems/ruby-1.9.3-p0/gems/activerecord-3.1.3/lib/active_record/base.rb:1567:in `initialize'
        from (irb):3:in `new'
        from (irb):3
        from /Volumes/Terra-Nova/jwaldrip/.rvm/gems/ruby-1.9.3-p0/gems/railties-3.1.3/lib/rails/commands/console.rb:45:in `start'
        from /Volumes/Terra-Nova/jwaldrip/.rvm/gems/ruby-1.9.3-p0/gems/railties-3.1.3/lib/rails/commands/console.rb:8:in `start'
        from /Volumes/Terra-Nova/jwaldrip/.rvm/gems/ruby-1.9.3-p0/gems/railties-3.1.3/lib/rails/commands.rb:40:in `<top (required)>'
        from script/rails:6:in `require'
        from script/rails:6:in `<main>'
app/models/user.rb
class User < ActiveRecord::Base

  # Attributes
  attr_accessible :username, :email, :password, :password_confirmation, :is_admin
  attr_accessor :password

  # Callbacks
  before_save :encrypt_password

  # Relationships
  has_many :irks

  # Validation
  validates_confirmation_of :password
  validates_presence_of :password, on: :create
  validates :password, presence: true, length: { in: 3..20 }

  validates :username, presence: true, uniqueness: true, length: { in: 3..20 }
  validates :email, presence: true, email: true, uniqueness: true

  # User Authentication
  def self.authenticate(email, password)
    user = find_by_email(email)
    if user && user.password_hash == BCrypt::Engine.hash_secret(password, user.password_salt)
      user
    else
      nil
    end
  end

  # Password Encryption
  def encrypt_password
    if password.present?
      self.password_salt = BCrypt::Engine.generate_salt
      self.password_hash = BCrypt::Engine.hash_secret(password, password_salt)
    end
  end
end
config/initializers/accessible_attributes.rb
class ActiveRecord::Base
  attr_accessible
  attr_accessor :accessible
  
  private
  def mass_assignment_authorizer
    if accessible == :all
      self.class.protected_attributes
    else
      super + (accessible || [])
    end
  end
end