Show custom message only on sign_in

2019-07-17 05:46发布

问题:

I am trying to figure out how I can show a custom "Welcome back" message in my view when a user signs in within my application.

I am currently using Devise to handle all of this and I can't seem to figure it out. Any help would be really appreciated.

回答1:

Flash

You'll want to use the flash system to create a "notice" that will be fired on the sessions#create controller of Devise:

#config/routes.rb
devise_for :user, controllers: { sessions: "users/sessions" } #=> add extra method to your sessions controller

#app/controllers/users/sessions_controller.rb
Class Users::SessionsController < Devise::SessionsController
   after_action :welcome_message, only: :create

   private

   def welcome_message
      flash[:notice] = "Welcome Back"
   end
end

This allows you to create a "Welcome Back" message, which will be inserted after the create method in your SessionsController. Nothing is overwritten in Devise - you're just adding a flash message after the user signs in