[Ruby] Flash message using devise

1 minute read

Description using # haml When you sign in or log out, you can display a message such as “Login.” “Logged out.” If you use devise, the message to be displayed when you log in is automatically stored in the “flash object”.

Within the flash object, multiple messages are stored in “key” and “value” format like a hash.

① Prepare view for message

Place the view file _notifications.html.haml for flash messages in views/layouts and read it with render in the body of application.html.haml.


  -flash.each do |key, value|
    = content_tag :div, value, class: key


    = render'layouts/notifications'
    = yield

② Perform flash message styling

By default, flash creates two keys, “notice (when operation succeeds)” and “alert (when failure)”. Apply style to each


.Notification {
  .notice {
    background-color: #38AEF0;
    color: #fff;
    text-align: center;

  .alert {
    background-color: #F35500;
    color: #fff;
    text-align: center;

Finally edit application.scss and read the created configuration file


@import "modules/flash";

③ Message localization

First, create a file for Japaneseization In the “config/locales” folder, create new files named “devise.ja.yml” and “ja.yml”.

Since the description content is posted on the following site, copy and paste the whole contents of each and save it. devise.ja.yml ja.yml

Next, let’s edit application.rb and change the language setting.


  class Application <Rails::Application
    config.i18n.default_locale = :ja