加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-17 03:32:19 所属栏目:百科 来源:网络整理
导读:我正在尝试在我的应用程序中添加一个身份验证系统,当我运行“rake db:migrate”时,我遇到了这个错误: NoMethodError: undefined method `attr_accessible' for User (call 'User.connection' to establish a connection):Class/Users/liferaymac/Desktop/c
我正在尝试在我的应用程序中添加一个身份验证系统,当我运行“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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读