【问题标题】:Devise authentication with ActionController::Metal使用 ActionController::Metal 设计身份验证
【发布时间】:2025-12-04 12:00:01
【问题描述】:

我需要制作一个带有 Devise 身份验证的 API。 API 最好用ActionController::Metal descendants 实现,但Devise 依赖于before_filter 方法,该方法仅在ActionController::Base descendants 中定义。

有什么解决办法吗?

【问题讨论】:

    标签: ruby-on-rails api authentication devise actioncontroller


    【解决方案1】:

    只需在您的 ApplicationController 中包含一些帮助程序

    例子:

    class ApplicationController < ActionController::Metal
        include AbstractController::Callbacks # This adds before_filter
        include Devise::Controllers::Helpers # This adds current_user
        ...
    end
    

    【讨论】:

    • 我还必须包含include AbstractController::Helpers