在哪里设计“ authenticate_user!”的实现?方法?


Answers:


77

它在lib/devise/controllers/helpers.rb1中并且是动态生成的(用户只是可能的后缀之一):

def self.define_helpers(mapping) #:nodoc:
    mapping = mapping.name

    class_eval <<-METHODS, __FILE__, __LINE__ + 1
      def authenticate_#{mapping}!(opts={})
        opts[:scope] = :#{mapping}
        warden.authenticate!(opts) if !devise_controller? || opts.delete(:force)
      end

      def #{mapping}_signed_in?
        !!current_#{mapping}
      end

      def current_#{mapping}
        @current_#{mapping} ||= warden.authenticate(:scope => :#{mapping})
      end

      def #{mapping}_session
        current_#{mapping} && warden.session(:#{mapping})
      end
    METHODS

    ActiveSupport.on_load(:action_controller) do
      helper_method "current_#{mapping}", "#{mapping}_signed_in?", "#{mapping}_session"
    end
  end


1
所以我该如何明确地访问它。此呼叫无效>>Devise::Controllers::Helpers.authenticate_user!
vipin8169'3

23

在将设计添加到rails时,通常会添加config/routes.rb

devise_for :user

这是在Devise Mapper类中定义的。

要求Devise.add_mapping将每种资源传递给devise_for

Devise模块的add_mapping方法在此处定义,随后调用define_helpers,该方法的定义authenticate如其他答案中所述。


By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.