简体   繁体   English

设计覆盖会话控制器

[英]devise overriding sessions controller

I want to add a message when user tries to sign in and it's not confirmed, I want to display this message in notice section, basically devise needs to provide this message to us but I don't see any message in this cases. 我想在用户尝试登录且未确认时添加一条消息,我想在通知部分中显示此消息,基本上是设计需要向我们提供此消息,但在这种情况下我看不到任何消息。 So I decided to add it manually from sessions controller here is my code: 因此,我决定从会话控制器手动添加它,这是我的代码:

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

the problem is flash[:notice] is empty after action is executed, in console I have 问题是执行操作后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

how can I display this message? 如何显示此消息?

More simple way to do what are asking about is to add before_filter :confirmation_notice in you application_controller and check whether current_user.confirmed? 更简单的方法是在您的application_controller中添加before_filter:confirmation_notice并检查current_user.confirmed? or not. 或不。

ex :: 前::

before_filter :confirmation_notice

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

Do not forget to configure your views to show this flash notice. 不要忘记配置视图以显示此闪动通知。

This to learn how to configure your views. 将学习如何配置视图。

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

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