简体   繁体   English

设计-无法在Application Controller中获取current_user

[英]Devise - can not get current_user in Application Controller

I'm using Devise for my application (Ruby 2.0, Rails 4). 我正在将Devise用于我的应用程序(Ruby 2.0,Rails 4)。 Everything run well (register, login, etc..). 一切运行良好(注册,登录等)。 However when i want to get current_user in ApplicationController it always return nil (already logged-in). 但是,当我想在ApplicationController中获取current_user时,它总是返回nil(已登录)。 Here is my code 这是我的代码

class ApplicationController < ActionController::Base
  before_action :set_locale

  def set_locale

    locale = detect_location
    if current_user && current_user.locale.blank?
      current_user.locale = locale
      current_user.save
    end

    I18n.locale = locale
  end

end

Any help? 有什么帮助吗?

Thank you, 谢谢,

Are you authenticating on other controllers and not this one? 您是否正在其他控制器上进行身份验证,而不是在此控制器上?

eg using before_filter :authenticate_user! 例如,使用before_filter :authenticate_user! in the other controllers? 在其他控制器?

def set_locale
  I18n.locale =
    unless user_signed_in?
      params[:locale] || 'en'
    else
      current_user[:locale] || 'en'
    end
end

I 一世

  1. In User Table, there's a field called 'locale'. 在用户表中,有一个名为“ locale”的字段。
  2. Noted that user_signed_in? 注意到user_signed_in吗? is a helper method from Devise Gem 是Devise Gem的辅助方法

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM