【问题标题】:devise overriding sessions controller设计覆盖会话控制器
【发布时间】:2014-11-18 21:19:30
【问题描述】:

我想在用户尝试登录但未确认时添加一条消息,我想在通知部分显示此消息,基本上设计需要向我们提供此消息但在这种情况下我没有看到任何消息.所以我决定从会话控制器手动添加它是我的代码:

class SessionsController < Devise::SessionsController

  def new
    super
  end

  def create
    user = User.find_by_email(params[:user][:email])

    self.resource = warden.authenticate!(auth_options)
    set_flash_message(:notice, :signed_in) if is_flashing_format?
    sign_in(resource_name, resource)

    if user.confirmed_at.nil?
      flash[:notice] = "my message here"
    end
    yield resource if block_given?

    respond_with resource, location: after_sign_in_path_for(resource)
  end

end

问题是执行操作后 flash[:notice] 为空,在控制台中我有

Started POST "/users/sign_in" for 127.0.0.1 at 2014-11-18 15:07:21 +0200
Processing by SessionsController#create as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"xC86tz4kZjcSMqXOL/+qpwlh5VlSbnsvLj93N5jb3NI=", "user"=>{"email"=>"pers.maki.5@gmail.com", "password"=>"[FILTERED]"}, "commit"=>"Sign in"}
   (0.2ms)  SELECT COUNT(*) FROM "landing_page_reports" 
  LandingPageReport Load (0.2ms)  SELECT "landing_page_reports".* FROM "landing_page_reports" ORDER BY "landing_page_reports"."id" DESC LIMIT 1
  User Load (0.4ms)  SELECT "users".* FROM "users" WHERE "users"."email" = 'pers.maki.5@gmail.com' LIMIT 1
  CACHE (0.0ms)  SELECT "users".* FROM "users" WHERE "users"."email" = 'pers.maki.5@gmail.com' LIMIT 1
   (0.1ms)  begin transaction
   (0.2ms)  commit transaction
Completed 401 Unauthorized in 193ms

如何显示此消息?

【问题讨论】:

    标签: ruby-on-rails devise


    【解决方案1】:

    更简单的方法是在你的 application_controller 中添加 before_filter :confirmation_notice 并检查 current_user.confirmed?与否。

    前::

    before_filter :confirmation_notice
    
    def confirmation_notice
      flash[:notice] = "my message here" unless current_user.confirmed?
    end
    

    不要忘记配置您的视图以显示此 Flash 通知。

    This 了解如何配置您的视图。

    【讨论】:

    • flash 在页面上仍然是空的
    • 您是否像提供的链接一样在视图中添加了 Flash 上的循环?
    • 我的 flash 对象是这样的 #<:flash::flashhash:0x007f16543d3d78>, @closed=false, @flashes={:alert= >“您必须在继续之前确认您的帐户。”}, @now=nil>
    • 这意味着您需要确认,这意味着没有人可以在不确认电子邮件的情况下登录。要在不确认的情况下允许登录,请使用 .skip_confirmation!看看stackoverflow.com/questions/13818280/…
    • 是否需要覆盖设计确认?模型中的方法。
    猜你喜欢
    • 1970-01-01
    • 2011-12-25
    • 1970-01-01
    • 1970-01-01
    • 2015-04-26
    • 2011-04-02
    • 1970-01-01
    • 2012-07-18
    • 1970-01-01
    相关资源
    最近更新 更多