我正在尝试在我的应用中添加身份验证系统,当我运行“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)
这是我的user.rb文件的视图,这是我的模型:
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
这是我的迁移文件:
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
我在http://railscasts.com/episodes/209-introducing-devise?autoplay=true工作 我也运行了bundle install并且已经安装了devise gem。 任何见解将不胜感激。
答案 0 :(得分:9)
来自Rails指南:
Rails 4.0已删除了attr_accessible和attr_protected功能 赞成强参数。您可以使用Protected Attributes gem 为了顺利升级路径。
来源:http://guides.rubyonrails.org/upgrading_ruby_on_rails.html
您可能希望保留attr_accessible方法,否则在使用注册表单创建用户时会遇到错误。您可以捆绑protected_attributes gem,以便访问模型中的这些方法。
答案 1 :(得分:0)
我意识到在Rails 4+中没有&#34; attr_accessible&#34;。我从模型中删除了它,现在它可以工作了。 (应该从一个旧视频中预料到这一点......)