在帮助文件中设计方法?

Devise methods in helper file?

我有一个辅助模块 ModelHelper。我想使用 user_signed_in?该辅助模块中的方法。但它显示错误。我如何在帮助程序文件中调用此方法。

我认为你可以使用 current_user.present? 无论如何,rails 助手非常丑陋,我建议完全不要使用它

方法 user_signed_in?Devise::Controllers::Helpers::ClassModule 模块中定义。长话短说,它只是检查 scope 是否在 warden 中进行了身份验证。所以你可以尝试在没有 Devise helpers

的情况下检查它
def #{mapping}_signed_in?
  !!current_#{mapping}
end

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