[RUBY] Flash message using devise

Description using haml

When you sign in or log out, you can display a message such as "Login." "Logout." At the top of the screen. If you use devise, the message to be displayed at login etc. is automatically stored in the "flash object".

Within the flash object, multiple messages are stored in "key" and "value" formats like a hash.

① Prepare a view for the message

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

ruby:_notifications.html.haml


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

ruby:application.html.haml


%body
    = render 'layouts/notifications'
    = yield

② Styling the flash message

By default, flash creates two keys, "notice" and "alert". Apply style to each

stylesheets/modules/_flash.scss


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

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

Finally, edit application.scss and load the created configuration file

app/assets/stylesheets/application.scss


@import "modules/flash";

③ Japaneseization of the message

First, create a file for Japanese localization Create new files named "devise.ja.yml" and "ja.yml" in the "config / locales" folder.

The contents are posted on the following sites, so copy and paste the entire contents and save. devise.ja.yml ja.yml

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

config/application.rb


  class Application < Rails::Application
    #~abridgement~
    config.i18n.default_locale = :ja
  end
end

Recommended Posts

Flash message using devise
Rails Flash Message
Freeters everywhere tried using devise.
Implement user management functionality using Devise
[Rails] Manage multiple models using devise gem
Implementation of user authentication function using devise (2)
Creating a user authentication function using devise
Implementation of user authentication function using devise (1)
Implementation of user authentication function using devise (3)
Get the error message using the any? method