[RUBY] Rails Flash Message

Flash Message implementation

1. View creation

haml:_notification.html.haml


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

image.png

--When using devise, a flash object is automatically created and multiple messages are saved with key :, value: values. --By using each statement, these are taken out one by one and displayed by using "key" as "class name for CSS" and "value" as "message body to be displayed". --Display on screen

haml:application.html.haml


%body
 = render 'layouts/notifications'
 = yield

↑ Since the view file of each page is displayed in the yield part, write = render'layouts / notifications' on it to display the flash message at the top of the page. ↓ image.png

  1. Flash Message Styling

stylesheets/modules/flash.scss


.notification {
  .notice {
    background-color: $light_blue;
    color: $white;
    text-align: center;
  }

  .alert {
    background-color: $alert_orange;
    color: $white;
    text-align: center;
  }
}
//Separate notification and alert messages

application.scss


@import "reset";
@import "config/colors";
@import "modules/flash";
//flash.Reflect scss
//reset.Described under the css file name defined as css

3. Japanese localization of Flash Message

--Create new files named "devise.ja.yml" and "ja.yml" in the "config / locales" folder

config/application.rb


module ChatSpace
  class Application < Rails::Application
    #~abridgement~
    config.i18n.default_locale = :ja
  end
end
//config/application.Add one sentence to rb and set Japanese

Recommended Posts

Rails Flash Message
Flash message using devise
Rails ~ Understanding the message function ~
Display Flash messages in Rails
[Rails] What was the error message?
[Rails] How to use flash messages
[Rails g.error]
Rails basics
Rails Review 1
[Rails] first_or_initialize
rails tutorial
About Rails 6
Rails foundation
Rails memorandum
rails tutorial
rails tutorial
rails tutorial
[Rails] devise
rails tutorial
rails tutorial
Rails Tips
rails method
rails tutorial
[Rails] ActiveRecord
[Rails] form_with
Rails Review 2
I don't see the flash message with redirect_to
[Rails] Let's translate the error message into Japanese
Fade out Rails flash without moving other content
[Rails] Japanese localization of validation error message ~ ja.yml ~