21

我正在设置设计并且需要能够使用设计助手。当我添加 before_filter :authenticate_user!到应用程序控制器时,我收到以下错误undefined method 'authenticate_user!' for #<HomeController:*>

它不直接存在于我的家庭控制器中,而是从应用程序控制器继承,如果我skip_before_filter :authenticate_user!在我的家庭(或任何控制器)中使用并尝试访问该页面,我会收到以下错误。

undefined method `user_signed_in?' for #<#<Class *>

它在调用它的 layouts/application.html.erb 文件中的那一行中断。任何助手都会发生同样的事情。看起来好像根本没有加载它们,但是,当我加载 rails 控制台并输入$LOAD_PATH.dup包含的输出时

 "/usr/local/rvm/gems/ruby-1.9.3-p429/gems/devise-2.2.4/lib", "/usr/local/rvm/gems/ruby-1.9.3-p429/gems/devise-2.2.4/app/controllers", "/usr/local/rvm/gems/ruby-1.9.3-p429/gems/devise-2.2.4/app/helpers", "/usr/local/rvm/gems/ruby-1.9.3-p429/gems/devise-2.2.4/app/mailers" 

因此,它们似乎确实应该由导轨加载。

这是我的应用程序控制器

类 ApplicationController < ActionController::Base

helper :all # 包括所有的助手,一直
protect_from_forgery
include ActionView::Helpers::NumberHelper
before_filter :authenticate_user!

结尾

安装设计后我确实重新启动了服务器,并且多次尝试不同的尝试来解决问题。如果需要更多文件或信息,请告诉我。非常感谢您提前。

编辑:我的 user.rb 文件被要求显示我确实安装了设计

class User < ActiveRecord::Base

  # Include default devise modules. Others available are:
  # :token_authenticatable, :confirmable,
  # :lockable, :timeoutable and :omniauthable
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable,
         :encryptable, :encryptor => :restful_authentication_sha1  

  # Setup accessible (or protected) attributes for your model  
  attr_accessible :email, :password, :password_confirmation, :remember_me  

end

编辑:整个错误消息

HomeController 中的 NoMethodError #index

未定义的方法“authenticate_user!” 对于 #< HomeController:0x000000040c45a0 > '

这是完整的跟踪

activesupport (3.2.13) lib/active_support/callbacks.rb:418:in ` run _2094683035699451847__process_action_ 2229546931289158113 _callbacks'

activesupport (3.2.13) lib/active_support/callbacks.rb:405:in `__run_callback'

activesupport (3.2.13) lib/active_support/callbacks.rb:385:in >`_run_process_action_callbacks'

activesupport (3.2.13) lib/active_support/callbacks.rb:81:in `run_callbacks'

actionpack (3.2.13) lib/abstract_controller/callbacks.rb:17:in `process_action'

actionpack (3.2.13) lib/action_controller/metal/rescue.rb:29:in `process_action'

actionpack (3.2.13) lib/action_controller/metal/instrumentation.rb:30:in `block in process_action'

activesupport (3.2.13) lib/active_support/notifications.rb:123:in `block in instrument'

activesupport (3.2.13) lib/active_support/notifications/instrumenter.rb:20:in `instrument'

activesupport (3.2.13) lib/active_support/notifications.rb:123:in `instrument'

actionpack (3.2.13) lib/action_controller/metal/instrumentation.rb:29:in `process_action'

actionpack (3.2.13) lib/action_controller/metal/params_wrapper.rb:207:in `process_action'

actionpack (3.2.13) lib/abstract_controller/base.rb:121:in `process'

actionpack (3.2.13) lib/abstract_controller/rendering.rb:45:in `process'

actionpack (3.2.13) lib/action_controller/metal.rb:203:in `dispatch'

actionpack (3.2.13) lib/action_controller/metal/rack_delegation.rb:14:in `dispatch'

actionpack (3.2.13) lib/action_controller/metal.rb:246:in `block in action'

actionpack (3.2.13) lib/action_dispatch/routing/route_set.rb:73:in `call'

actionpack (3.2.13) lib/action_dispatch/routing/route_set.rb:73:in `dispatch'

actionpack (3.2.13) lib/action_dispatch/routing/route_set.rb:36:in `call'

旅程 (1.0.4) lib/journey/router.rb:68:in `block in call'

旅程 (1.0.4) lib/journey/router.rb:56:in `each'

旅程 (1.0.4) lib/journey/router.rb:56:in `call'

actionpack (3.2.13) lib/action_dispatch/routing/route_set.rb:612:in `call'

典狱长 (1.2.1) lib/warden/manager.rb:35:in `block in call'

监狱长 (1.2.1) lib/warden/manager.rb:34:in `catch'

典狱长 (1.2.1) lib/warden/manager.rb:34:in `call'

actionpack (3.2.13) lib/action_dispatch/middleware/best_standards_support.rb:17:in `call'

机架 (1.4.5) lib/rack/etag.rb:23:in `call'

rack (1.4.5) lib/rack/conditionalget.rb:25:in `call'

actionpack (3.2.13) lib/action_dispatch/middleware/head.rb:14:in `call'

actionpack (3.2.13) lib/action_dispatch/middleware/params_parser.rb:21:in `call'

actionpack (3.2.13) lib/action_dispatch/middleware/flash.rb:242:in `call'

rack (1.4.5) lib/rack/session/abstract/id.rb:210:in `context'

rack (1.4.5) lib/rack/session/abstract/id.rb:205:in `call'

actionpack (3.2.13) lib/action_dispatch/middleware/cookies.rb:341:in `call'

activerecord (3.2.13) lib/active_record/query_cache.rb:64:in `call'

activerecord (3.2.13) lib/active_record/connection_adapters/abstract/connection_pool.rb:479:in `call'

actionpack (3.2.13) lib/action_dispatch/middleware/callbacks.rb:28:in `block in call'

activesupport (3.2.13) lib/active_support/callbacks.rb:405:in ` run _177144612565476380_ call _4534346825225857812__callbacks'

activesupport (3.2.13) lib/active_support/callbacks.rb:405:in `__run_callback'

activesupport (3.2.13) lib/active_support/callbacks.rb:385:in `_run_call_callbacks'

activesupport (3.2.13) lib/active_support/callbacks.rb:81:in `run_callbacks'

actionpack (3.2.13) lib/action_dispatch/middleware/callbacks.rb:27:in `call'

actionpack (3.2.13) lib/action_dispatch/middleware/reloader.rb:65:in `call'

actionpack (3.2.13) lib/action_dispatch/middleware/remote_ip.rb:31:in `call'

actionpack (3.2.13) lib/action_dispatch/middleware/debug_exceptions.rb:16:in `call'

actionpack (3.2.13) lib/action_dispatch/middleware/show_exceptions.rb:56:in `call'

railties (3.2.13) lib/rails/rack/logger.rb:32:in `call_app'

railties (3.2.13) lib/rails/rack/logger.rb:16:in `block in call'

activesupport (3.2.13) lib/active_support/tagged_logging.rb:22:in `tagged'

railties (3.2.13) lib/rails/rack/logger.rb:16:in `call'

actionpack (3.2.13) lib/action_dispatch/middleware/request_id.rb:22:in `call'

rack (1.4.5) lib/rack/methodoverride.rb:21:in `call'

rack (1.4.5) lib/rack/runtime.rb:17:in `call'

activesupport (3.2.13) lib/active_support/cache/strategy/local_cache.rb:72:in `call'

rack (1.4.5) lib/rack/lock.rb:15:in `call'

actionpack (3.2.13) lib/action_dispatch/middleware/static.rb:63:in `call'

railties (3.2.13) lib/rails/engine.rb:479:in `call'

railties (3.2.13) lib/rails/application.rb:223:in `call'

railties (3.2.13) lib/rails/railtie/configurable.rb:30:in `method_missing'

乘客 (4.0.2) lib/phusion_passenger/rack/thread_handler_extension.rb:77:in `process_request'

乘客 (4.0.2) lib/phusion_passenger/request_handler/thread_handler.rb:135:in `accept_and_process_next_request'

乘客 (4.0.2) lib/phusion_passenger/request_handler/thread_handler.rb:106:in `main_loop'

乘客 (4.0.2) lib/phusion_passenger/request_handler.rb:449:in `block (4 levels) in start_threads'

乘客 (4.0.2) lib/phusion_passenger/utils/robust_interruption.rb:108:in `disable_interruptions'

乘客 (4.0.2) lib/phusion_passenger/request_handler.rb:444:in `block (3 levels) in start_threads'

编辑:这是在控制台中运行 Devise.helpers 的文本。

$ 导轨控制台

加载开发环境(Rails 3.2.13)

1.9.3-p429:001 > 设计.helpers

=> #< 设置:{Devise::Controllers::Helpers} >

编辑:这是我的路线 rb 文件。:Application.routes.draw 做

devise_for :users  

resource :sessions, :only => [:new, :create, :destroy]

devise_scope :user do
    match 'signup' => 'users#new', :as => :signup
    match 'register' => 'users#create', :as => :register
    match '/login' => 'sessions#new', :as => :login 
     match 'logout' => 'sessions#destroy', :as => :logout
end
match '/activate/:activation_code' => 'users#activate', :as => :activate, :activation_code => nil

match '/users/:id', :to => 'users#show', :as => :user

resources :users do

member do
     put :suspend
     put :unsuspend
     delete :purge
end

end

resource :sessions

   other resources

match '' => 'home#index', :as => :home
match ':controller(/:action(/:id))'
root to: 'home#index'
end 
4

6 回答 6

34

请务必添加devise_for :user您的routes.rb

在此处查看更多信息:https ://stackoverflow.com/a/11180576

于 2014-03-10T14:07:54.500 回答
6

只需重新启动您的 Rails 服务器。

“请注意,如果你已经启动了你的应用程序,你应该在这里重新启动它。否则你会遇到奇怪的错误,比如用户无法登录和路由助手未定义。” [入门说明]

[编辑]更详细的答案:

请验证另外两件事:您的 HomeController 应该继承自 Devise::Controllers::Helpers。route.rb 中的 devise 指令(在 Helpers 中生成缺失的方法)应该扩展了 Devise.@@mappings。以下代码可以检查这一点:

class ApplicationController ...
  ...
  before_filter do
    fail "bad ancestor" unless self.kind_of?(Devise::Controllers::Helpers)
    fail "no mapping" unless Devise.class_variable_get(:@@mappings).[:user]
    authenticate_user!
  end
end

要在工作的 rails 应用程序中引入类似的问题,必须禁用 config/initializers/devise.rb 和 config/routes.rb 设计条目,并且必须重新启动 rails。除非重新启动 rails,否则重新启用代码不会解决问题。

于 2013-07-27T17:49:57.700 回答
2

有很多事情可能出错。需要按顺序检查的一些事项:

  • 无法调用before_filter :authenticate_user!表示未正确设置设备。
  • 添加devise_for :usersroutes.rb (它是 user s,你做对了),接下来简化那些范围规则以进行测试。
  • 您需要before_filter :authenticate_user!努力在控制器中获取助手或手动包含助手
  • 如果您的基本控制器没有从 ActionController 继承,那么您需要手动添加辅助方法:将其放入您的基本控制器中helper_method "current_user", "user_signed_in?", "user_session"
  • 要检查的一件事:self.controller.current_user在你的erb中工作吗?这将表明 helper_methods 不包括在内。
于 2015-04-27T19:38:20.100 回答
2

I added this line to my controller which did the trick for me:

include Devise::Controllers::Helpers
于 2016-01-13T07:00:59.597 回答
0

我遇到了同样的问题(Rails 5.1.2,Devise 4.3.0),碰巧我正在使用:-

devise_for :user do
  ...
end

代替

devise_for :users do
  ...
end

我通过更改命名空间来修复它。

用户 => 用户

于 2017-08-10T14:39:19.820 回答
0

按照 Devise 的入门指南,我遇到了同样的问题。

原来我错误地运行rails generate model User而不是rails generate devise User,它只是创建了一个普通的 ActiveRecord 模型,它恰好具有名称“用户”。

于 2017-09-09T12:04:46.187 回答