【问题标题】:Devise methods in helper file?在帮助文件中设计方法?
【发布时间】:2020-02-17 10:05:09
【问题描述】:

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

【问题讨论】:

标签: ruby-on-rails devise


【解决方案1】:

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

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

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

【讨论】:

    【解决方案2】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-01-16
      • 2023-03-22
      • 1970-01-01
      • 2010-11-05
      • 2017-03-02
      • 1970-01-01
      相关资源
      最近更新 更多