簡體   English   中英

設計覆蓋會話控制器

[英]devise overriding sessions controller

我想在用戶嘗試登錄且未確認時添加一條消息,我想在通知部分中顯示此消息,基本上是設計需要向我們提供此消息,但在這種情況下我看不到任何消息。 因此,我決定從會話控制器手動添加它,這是我的代碼:

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

如何顯示此消息?

更簡單的方法是在您的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

不要忘記配置視圖以顯示此閃動通知。

將學習如何配置視圖。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM