微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐