Ruby-on-rails 用户的未定义方法“设计”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24148723/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
undefined method 'devise' for User
提问by JonleePeakman
I have been looking to get to grips with devise and its workings and have kind of fallen at the first hurdle. I have looked in a few places but cannot seem to find someone with this error exactly.
我一直在寻找掌握设计及其工作原理的方法,但在第一个障碍中有点失败。我查看了几个地方,但似乎无法准确找到有此错误的人。
So I have created a simple Home controller with an index view and added root 'home#index' and also ensured the default url options are setup in the development.rb file. I then simply typed:
因此,我创建了一个带有索引视图的简单 Home 控制器,并添加了 root 'home#index',并确保在 development.rb 文件中设置了默认的 url 选项。然后我简单地输入:
rails generate devise User
This created my user.rb file in models with the following:
这在具有以下内容的模型中创建了我的 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
Pretty straightforward so far, I have the following Gemfile:
到目前为止非常简单,我有以下 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'
And when I run either rake db:migrate I get the following error:
当我运行 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)>'
Im at a loss as to why the User model cannot find the 'devise' method when as far as I can see it is definitely there.
我不知道为什么 User 模型找不到“设计”方法,而据我所知,它肯定存在。
I get similar errors with rake routes, rails server and rails console.
我在使用 rake 路由、rails 服务器和 rails 控制台时遇到类似的错误。
For further info I am using ruby 2.1.1 if that helps?
有关更多信息,如果有帮助,我正在使用 ruby 2.1.1?
回答by Kirti Thorat
Add deviseto your application Gemfileand install it by running bundle install. After this, you should run the following generator command:
添加devise到您的应用程序Gemfile并通过运行安装它bundle install。在此之后,您应该运行以下生成器命令:
rails generate devise:install
This generator will install an initializer your_application/config/initializers/devise.rbwhich consists of all the Devise's configuration options.
此生成器将安装一个初始化程序your_application/config/initializers/devise.rb,其中包含所有设计的配置选项。
You missed the above mentioned step which is why the devise configurations are not set and you receive undefined method 'devise' for Usererror in your model class User.
您错过了上述步骤,这就是为什么未设置设计配置并且您undefined method 'devise' for User在模型类中收到错误的原因User。
回答by Stefan Ciprian Hotoleanu
I ran into a similar issue when I was configuring Devise (Ruby 2.4.1 / Rails 5.1.2). In my case it seems that the following files were not created after I executed: rails generate devise:installfor the first time.
我在配置 Devise(Ruby 2.4.1 / Rails 5.1.2)时遇到了类似的问题。在我的情况下,似乎在我执行后没有创建以下文件:rails generate devise:install第一次。
create config/initializers/devise.rb
create config/locales/devise.en.yml
Steps that I followed:
我遵循的步骤:
1) Comment from your MODEL the following:
1)从您的模型评论以下内容:
#devise :database_authenticatable, :registerable,
#:recoverable, :rememberable, :trackable, :validatable
2) Comment from routes.rb:
2)来自 routes.rb 的评论:
#devise_for :sessions
3) Run rails generate devise:installagain, you should see that some files are created this time. Hope you it works !
3)rails generate devise:install再次运行,你应该看到这次创建了一些文件。希望你它有效!
4) Uncomment from 1) & 2)
4) 取消注释 1) & 2)
5) Execute: rake db:migrate
5)执行: rake db:migrate
And at this point it should work. Hope it helps someone !
在这一点上它应该工作。希望它可以帮助某人!
回答by mmike
I have the same issue but with other reason. this can also be problem for somebody. Stop rails server and then type
我有同样的问题,但有其他原因。这对某人来说也可能是个问题。停止 Rails 服务器,然后键入
rails s
to restart it
要重新启动
回答by abla
I've run the generator $ rails generate devise:installbut got the same issue.
我已经运行了发电机,$ rails generate devise:install但遇到了同样的问题。
Anyway it works to me: Add extend Devise::Modelsto the User models.
无论如何它对我有用:添加extend Devise::Models到用户模型。
回答by Kevin Kohrt
In addition to Kirti's answer of running
除了 Kirti 的跑步回答
rails generate devise:install
you may want to rename the devise initializer to
您可能希望将设计初始值设定项重命名为
config/initializers/01_devise.rb
because if any other initializer (such as config/initializers/active_admin.rb) runs before devise and touches the ApplicationController, you will get the same error.
因为如果任何其他初始化程序(例如 config/initializers/active_admin.rb)在设计之前运行并触及 ApplicationController,您将得到相同的错误。
And according to http://guides.rubyonrails.org/configuring.html#using-initializer-filesyou control the load order of initializers using file naming.
根据 http://guides.rubyonrails.org/configuring.html#using-initializer-files,您可以使用文件命名控制初始化程序的加载顺序。
回答by Artur Bilski
I had the same issue. The fix was to uncomment:
我遇到过同样的问题。解决方法是取消注释:
require 'devise/orm/active_record'
in config/initializers/active_admin.rb.
在config/initializers/active_admin.rb。
I deliberately commented it out earlier because documentation says: "Supports :active_record (default)".
我之前故意将其注释掉,因为文档说:“支持:active_record(默认)”。
回答by Sam Stickland
So I just had this same error crop up when I did:
所以当我这样做时,我刚刚出现了同样的错误:
rails g controller client/timesheets
rails g 控制器客户端/时间表
It turned out that the problem was with the generated helper:
原来问题出在生成的助手上:
app/helpers/client/timesheet_helper.rb:
应用程序/助手/客户端/timesheet_helper.rb:
module Client::TimesheetHelper
end
Client is a model that uses Single Table Inheritance off of the User model. Something was getting very mixed up her, what with Client being a Model (class) and a module, and it results in this cryptic error message.
客户端是一个使用单表继承脱离用户模型的模型。有些东西让她很困惑,客户端是一个模型(类)和一个模块,这导致了这个神秘的错误消息。
YMMV.
天啊。
回答by nachbar
I got the same error when I installed Devise and Mongoid, but forgot to change the orm from active_record to mongoid in config/initializers/devise.rb
我在安装 Devise 和 Mongoid 时遇到了同样的错误,但忘记将 config/initializers/devise.rb 中的 orm 从 active_record 更改为 mongoid
Should be:
应该:
require 'devise/orm/mongoid'
回答by Szymon Rut
I had to stop the Spring preloader process:
我不得不停止 Spring 预加载器进程:
spring stop
Now works as expected
现在按预期工作
回答by Bryan Memeti
Comment out:-
注释掉:-
devise :database_authenticatable, :registerable, :recoverable, :rememberable, :trackable, :validatable
in user.rbModel. This worked for me
在user.rb模型中。这对我有用

