为什么此devise_group调用会出现NoMethodError?
我正在使用devise 3.5.2,ruby 2.2.2,rails 4.2.3
I'm using devise 3.5.2, ruby 2.2.2, rails 4.2.3
class ApplicationController < ActionController::Base
# Prevent CSRF attacks by raising an exception.
protect_from_forgery with: :exception
devise_group :user, contains: [:manager, :employee]
end
我的理解是Devise应该加载这些帮助程序自动完成,我应该能够在ApplicationController中使用它们而无需做任何特殊的事情。
My understanding is that Devise should load these helpers automatically and I should be able to use them in ApplicationController without doing anything special.
我尝试运行带有 rails s的应用程序,我得到此错误:
I try and run the app with 'rails s' and I get this error:
multi-user-role-devise / app / controllers / application_controller.rb: 6:在
< class:ApplicationController>'中:未定义方法
devise_group'for
ApplicationController:Class(NoMethodError)from
/ home /me/ror-code/multi-user-role-devise/app/controllers/application_controller.rb:1:in
`'.... etc
multi-user-role-devise/app/controllers/application_controller.rb:6:in
<class:ApplicationController>': undefined method
devise_group' for ApplicationController:Class (NoMethodError) from /home/me/ror-code/multi-user-role-devise/app/controllers/application_controller.rb:1:in `' ....etc
我该如何解决?
Gemfile:
source 'https://rubygems.org'
ruby '2.2.2'
gem 'rails', '4.2.3'
gem 'sqlite3'
gem 'sass-rails', '~> 5.0'
gem 'uglifier', '>= 1.3.0'
gem 'coffee-rails', '~> 4.1.0'
gem 'jquery-rails'
gem 'turbolinks'
gem 'jbuilder', '~> 2.0'
gem 'foundation-rails'
gem 'simple_form'
gem 'devise'
group :development do
gem 'better_errors'
gem 'quiet_assets'
gem 'rails_layout'
gem 'spring-commands-rspec'
end
group :development, :test do
gem 'factory_girl_rails'
gem 'faker'
gem 'rspec-rails'
gem 'byebug'
gem 'web-console', '~> 2.0'
gem 'spring'
end
group :test do
gem 'capybara'
gem 'database_cleaner'
gem 'launchy'
gem 'selenium-webdriver'
end
管理器类:
class Manager < User
end
员工类别:
class Employee < User
end
如果您仔细阅读了 Devise问题,您会发现遇到相同问题的人。最后,您会发现可能的 devise_permitted_parameters.rb
初始化程序阻止了所有Devise助手的正确加载。通过删除此初始化程序并在控制器中清除参数,可以解决该问题。
If you read through this Devise issue you will find people with the same problem. At the end, you will find out, that a possible devise_permitted_parameters.rb
initializer prevents the correct loading of all Devise helpers. By removing this initializer and sanitizing your parameters within your controller should solve the problem.
同时,您可以在 include Devise :: Controllers :: Helpers :: ClassMethods c $ c> ApplicationController ,但通常应避免这样的编码样式。
In the meanwhile, you can add include Devise::Controllers::Helpers::ClassMethods
within your ApplicationController
but you should avoid coding style like this in general.