Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

rails undefined method `attr_accessible'

I'm trying to add an authentication system into my app and I've run into this error when I run "rake db:migrate":

NoMethodError: undefined method `attr_accessible' for User (call 'User.connection' to establish a connection):Class
/Users/liferaymac/Desktop/check/app/models/user.rb:8:in `<class:User>'
/Users/liferaymac/Desktop/check/app/models/user.rb:1:in `<top (required)>'
/Users/liferaymac/Desktop/check/config/routes.rb:2:in `block in <top (required)>'
/Users/liferaymac/Desktop/check/config/routes.rb:1:in `<top (required)>'
/Users/liferaymac/Desktop/check/config/environment.rb:5:in `<top (required)>'
Tasks: TOP => db:migrate => environment
(See full trace by running task with --trace)

Here is a view of my user.rb file, which is my model:

class User < ActiveRecord::Base
  # Include default devise modules. Others available are:
  # :confirmable, :lockable, :timeoutable and :omniauthable
  devise :database_authenticatable, :registerable,
     :recoverable, :rememberable, :trackable, :validatable

  # Setup accessible (or protected) attributes for your model
  attr_accessible :email, :password, :password_confirmation
end

And here is my migrate file:

class DeviseCreateUsers < ActiveRecord::Migration
  def self.up
    create_table(:users) do |t|
      t.database_authenticatable :null => false
      # t.confirmable
      t.recoverable
      t.rememberable
      t.trackable
      # t.lockable :lock_strategy => :failed_attempts, :unlock_strategy => :both

      t.timestamps
    end

    add_index :users, :email,                :unique => true
    # add_index :users, :confirmation_token,   :unique => true
    add_index :users, :reset_password_token, :unique => true
    # add_index :users, :unlock_token,         :unique => true
  end

  def self.down
    drop_table :users
  end
end

# migration
create_table(:users) do |t|
  t.database_authenticatable :null => false
  # t.confirmable
  t.recoverable
  t.rememberable
  t.trackable
  # t.lockable :lock_strategy => :failed_attempts, :unlock_strategy => :both

  t.timestamps
end

add_index :users, :email,                :unique => true
# add_index :users, :confirmation_token,   :unique => true
add_index :users, :reset_password_token, :unique => true
# add_index :users, :unlock_token,         :unique => true

I'm working from http://railscasts.com/episodes/209-introducing-devise?autoplay=true I also ran bundle install and installed the devise gem already. Any insight will be greatly appreciated.

like image 511
Paul Avatar asked Aug 26 '14 03:08

Paul


2 Answers

From the Rails Guides:

Rails 4.0 has removed attr_accessible and attr_protected feature in favor of Strong Parameters. You can use the Protected Attributes gem for a smooth upgrade path.

Source: http://guides.rubyonrails.org/upgrading_ruby_on_rails.html

You will probably want to keep the attr_accessible method or you will run into errors when creating users with a sign up form. You can bundle the protected_attributes gem to give you access to those methods in your models.

Protected Attributes: https://github.com/rails/protected_attributes

like image 153
sealocal Avatar answered Nov 20 '22 13:11

sealocal


I realized that in Rails 4+ there is no "attr_accessible". I deleted it out of my model and now it works. (Should have expected this from an old video....)

like image 1
Paul Avatar answered Nov 20 '22 15:11

Paul