仅在sign_in上显示自定义消息 [英] Show custom message only on sign_in

查看:94
本文介绍了仅在sign_in上显示自定义消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图弄清楚当用户在我的应用程序中登录时如何在视图中显示自定义的欢迎回来消息。

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.

我目前正在使用Devise来处理所有这一切,但似乎无法弄清楚。任何帮助将不胜感激。

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.

推荐答案

Flash

您将要使用 flash 系统创建一个通知,该通知将在Devise的 sessions#create 控制器上触发:

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

这可让您创建欢迎回来消息,它将插入到 SessionsController 中的 create 方法之后。 Devise 中没有任何内容被覆盖-您只是在用户登录后添加即显消息

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

这篇关于仅在sign_in上显示自定义消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆