[Rails] How to use flash messages

Introduction

Flash is a function that temporarily displays a simple message when a page is changed. For example, when a user successfully logs in, "I was able to log in. ] Is displayed so that the user can check the progress.

table of contents

1 Basic writing 2 Difference between flash and flash.now

1. Basic writing

Editing the controller

Flash is treated like a hash.

flash[:Key name] =“Message to be displayed”

For the key name, use the previously prepared notice or alert option (you can give it any name you like).

Example

controlle.rb


if @outgo.update(outgo_params)
   flash[:alert] ="Message" has been registered. ’
   redirect_to root_path
else
        ~Abbreviation~  

Edit view

Write the following in the place where you want to display the flash message.

<%= flash[:Key name] %>

Example

html.erb


<%= flash[:alert] %>

2. Difference between flash and flash.now

Proper use of both

--flash is when displaying a flash message in the view file after the next action is taken (when using redirect_to) --flash.now is when you want to display a valid flash message only for the view file displayed by the current action (when using render)

Behavior of render and redirect_to

Reference link

https://pikawaka.com/rails/flash https://qiita.com/dice9494/items/2a0e92aba58a516e42e9

Recommended Posts

[Rails] How to use flash messages
[Rails] How to use enum
[Rails] How to use enum
How to use rails join
[Rails] How to use validation
[Rails] How to use authenticate_user!
[Rails] How to use "kaminari"
[Rails] How to use Scope
[Rails] How to use devise (Note)
How to use Ruby on Rails
[Rails] How to use Active Storage
[Introduction to Rails] How to use render
How to use custom helpers in rails
[Ruby on Rails] How to use CarrierWave
[Rails] How to use rails console with docker
[Rails] How to use ActiveRecord :: Bitemporal (BiTemporalDataModel)
[Rails] How to use the map method
[Rails] How to display error messages individually
How to use MySQL in Rails tutorial
[Ruby on Rails] How to use redirect_to
[Note] How to use Rails 6 Devise + cancancan
[Ruby on Rails] How to use kaminari
[Rails] How to use video_tag to display videos
[Rails] How to use helper method, confimartion
How to use credentials.yml.enc introduced in Rails 5.2
How to use Map
How to write Rails
How to use rbenv
How to use letter_opener_web
How to use with_option
How to use fields_for
How to use java.util.logging
How to use map
How to use collection_select
How to use Twitter4J
How to use active_hash! !!
How to use MapStruct
How to use hidden_field_tag
How to use TreeSet
How to uninstall Rails
[How to use label]
How to use identity
How to use hashes
How to use JUnit 5
How to use Dozer.mapper
How to use Gradle
How to use org.immutables
How to use java.util.stream.Collector
How to use VisualVM
How to use Map
[Ruby on Rails] How to display error messages
[Rails] How to use select boxes in Ransack
How to use rails g scaffold, functions, precautions
How to use JQuery in js.erb of Rails6
[Rails] How to use Gem'rails-i18n' for Japanese support
[Rails] How to get success and error messages
[Ruby on Rails] How to use session method
[Rails] How to use PostgreSQL in Vagrant environment
[rails] How to post images
How to use Chain API
[Java] How to use Map