1

I tried the following but it's not working. It's still using the default flash message.

class Users::SessionsController < Devise::SessionsController
  after_action :custom_welcome, :only => [:create]

  def custom_welcome
    flash.notice = "Welcome back "+current_user.name+"." if flash.keys.include?(:notice)
  end
end

Reference: https://github.com/plataformatec/devise#configuring-controllers

https://stackoverflow.com/a/5513172/148844

VC.One
  • 14,790
  • 4
  • 25
  • 57
Chloe
  • 25,162
  • 40
  • 190
  • 357

4 Answers4

3

Docs say (in general):
1. Set custom flash messages with :key in locales: config/locales/devise.en.yml
2. Call DeviseController method set_flash_message(key, kind, options = {})

Example from devise/sessions_controller:
set_flash_message!(:signed_in, :notice)

Denis
  • 940
  • 12
  • 16
1

Two ways:

  1. Use your own layout for Devise pages (layout keyword with parameter in controller). In layout you can have separate partial to show your devise messages.

  2. If your app is bit complicated and you don't want to introduce layouts, create devise controllers (it can be done by running devise rake command), then just have a flag in your flash.

For example:

flash[:devise] = true

Check for this flag in your partial view responsible for generating html for your flash messages:

<% if flash[:devise] %>
  #...
<% else %>
  # ...
<% end %>
Roman Pushkin
  • 5,639
  • 3
  • 40
  • 58
1

This worked.

flash.notice = "Welcome back #{current_user.name}." if flash.key?(:notice)
Sebastián Palma
  • 32,692
  • 6
  • 40
  • 59
Chloe
  • 25,162
  • 40
  • 190
  • 357
1

You're overriding a devise controller, therefor you need to tell your app that you want to use that controller. Do the following in routes.rb (if you haven't done so already):

devise_for :users, controllers: { sessions: 'users/sessions' }

To expand on this, if you're still getting the default flash message, it's because Devise is still using the original controllers, by defining the customized controller you are telling it to use it.

Dotol
  • 376
  • 1
  • 4
  • 19