ruby-on-rails – rails undefined方法`attr_accessible’

我正在尝试在我的应用程序中添加一个身份验证系统,当我运行“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,: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,:unique => true
# add_index :users,:unique => true
add_index :users,:unique => true

我从http://railscasts.com/episodes/209-introducing-devise?autoplay=true开始工作
我也运行了bundle install并且已经安装了devise gem.
任何见解将不胜感激.

解决方法

来自Rails指南:

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.

资料来源:http://guides.rubyonrails.org/upgrading_ruby_on_rails.html

您可能希望保留attr_accessible方法,否则在使用注册表单创建用户时会遇到错误.您可以捆绑protected_attributes gem,以便访问模型中的这些方法.

受保护的属性https://github.com/rails/protected_attributes

相关文章

validates:conclusion,:presence=>true,:inclusion=>{...
一、redis集群搭建redis3.0以前,提供了Sentinel工具来监控各...
分享一下我老师大神的人工智能教程。零基础!通俗易懂!风趣...
上一篇博文 ruby传参之引用类型 里边定义了一个方法名 mo...
一编程与编程语言 什么是编程语言? 能够被计算机所识别的表...
Ruby类和对象Ruby是一种完美的面向对象编程语言。面向对象编...