我一直在寻求掌握设计及其工作原理,但在第一个障碍上就倒下了。我已经查看了几个地方,但似乎无法找到确切存在此错误的人。
因此,我创建了一个带有索引 View 的简单 Home Controller ,并添加了根“home#index”,还确保在 development.rb 文件中设置了默认的 url 选项。然后我简单地输入:
rails generate devise User
这在具有以下内容的模型中创建了我的 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
end
到目前为止非常简单,我有以下 Gemfile:
source 'https://rubygems.org'
gem 'rails', '4.0.5'
gem 'sqlite3'
gem 'sass-rails', '~> 4.0.2'
gem 'devise'
gem 'uglifier', '>= 1.3.0'
gem 'coffee-rails', '~> 4.0.0'
gem 'jquery-rails'
gem 'turbolinks'
gem 'jbuilder', '~> 1.2'
group :doc do
gem 'sdoc', require: false
end
gem 'bcrypt'
当我运行 rake db:migrate 时,我得到以下错误:
rake aborted!
NoMethodError: undefined method `devise' for User (call 'User.connection' to establish a connection):Class
/home/jonlee/.rvm/gems/ruby-2.1.1@railstutorial_rails_4_0/gems/activerecord-4.0.5/lib/active_record/dynamic_matchers.rb:22:in `method_missing'
/home/jonlee/Projects/rails/userauth/app/models/user.rb:4:in `<class:User>'
/home/jonlee/Projects/rails/userauth/app/models/user.rb:1:in `<top (required)>'
我不知道为什么用户模型找不到“设计”方法,而据我所知它确实存在。
我在 rake 路由、rails 服务器和 rails 控制台中遇到了类似的错误。
有关更多信息,我正在使用 ruby 2.1.1 是否有帮助?
最佳答案
将 devise
添加到您的应用程序 Gemfile
并通过运行 bundle install
来安装它。在此之后,您应该运行以下生成器命令:
rails generate devise:install
此生成器将安装一个初始化程序 your_application/config/initializers/devise.rb
,其中包含 Devise 的所有配置选项。
您错过了上述步骤,这就是为什么未设置设计配置并且您在模型类 User
中收到 undefined method 'devise' for User
错误。
关于ruby-on-rails - 用户的未定义方法 'devise',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24148723/