[RUBY] [Rails] How to get rid of flash messages in a certain amount of time

Development environment

・ Ruby: 2.5.7 Rails: 5.2.4 ・ Vagrant: 2.2.7 -VirtualBox: 6.1 ・ OS: macOS Catalina

Implementation

Gemfile


gem 'jquery-rails'

Terminal


bundle install

controller



  def create
    @book = Book.new(book_params)
    @book.user_id = current_user.id
    if @book.save
      redirect_to book_path(@book)
      flash[:notice] = "The book was posted"
    else
      @books = Book.all
      flash.now[:alart_flash] = "Book posting failed"
      render 'index'
    end
  end

When the book is posted, it is named notice, and when it fails, it is named alart_flash. You can change the name here by yourself! When set to flash [:], the next action is displayed. If you set it to flash.now [:], it will disappear when you move to the next action. Note that render is not an action as it only calls the specified views. Note that redirect_to will be the next action, so it will not be displayed if it is flash.now.

Flash message decoration

application.scss


.flash{
  width: 100%;
  height: 30px;
  font-size: 18px;
  text-align: center;
  padding: 0;
  z-index: 1;
}

.notice{
  background-color: #65A2FF;
}

.alart_flash{
  color: #FFFFFF;
  background-color: #FF0000;
}

Turn off the flash message after a certain period of time.

application.js


//Flash message
$(function(){
  $('.flash').fadeOut(4000);  //Disappears over 4 seconds
});

Recommended Posts

[Rails] How to get rid of flash messages in a certain amount of time
[Rails] How to use flash messages
[jsoup] How to get the full amount of a document
How to insert a video in Rails
How to get the absolute path of a directory running in Java
How to implement a like feature in Rails
How to easily create a pull-down in Rails
How to use JQuery in js.erb of Rails6
[Rails] How to get success and error messages
How to make a follow function in Rails
How to get the ID of a user authenticated with Firebase in Swift
How to make a unique combination of data in the rails intermediate table
How to implement a like feature in Ajax in Rails
[How to insert a video in haml with Rails]
How to write a date comparison search in Rails
[Rails 6] How to set a background image in Rails [CSS]
[Rails] How to get the contents of strong parameters
[Rails] How to load JavaScript in a specific view
How to get started with creating a Rails app
Display Flash messages in Rails
How to get values in real time with TextWatcher (Android)
How to display a graph in Ruby on Rails (LazyHighChart)
[Rails] How to write in Japanese
How to get parameters in Spark
How to introduce jQuery in Rails 6
How to get along with Rails
How to install Swiper in Rails
[Webpacker] Summary of how to install Bootstrap and jQuery in Rails 6.0
How to delete large amounts of data in Rails and concerns
How to get the id of PRIMAY KEY auto_incremented in MyBatis
How to get boolean value with jQuery in rails simple form
How to rename a model with foreign key constraints in Rails
How to output a list of strings in JSF as comma-separated strings
How to get the length of an audio file in java
How to get JDK 11 on your mac in a comfortable way
How to implement search functionality in Rails
[Rails] I want to send data of different models in a form
How to change app name in rails
How to get date data in Ruby
How to use custom helpers in rails
How to implement a slideshow using slick in Rails (one by one & multiple by one)
How to create a query using variables in GraphQL [Using Ruby on Rails]
[Rails] How to log in with a name by adding a devise name column
[Rails] How to display error messages individually
How to use MySQL in Rails tutorial
How to update user edits in Rails Devise without entering a password
How to get the current date as a string in yyyyMMdd format
Steps to set a favicon in Rails
[Rails] How to get the user information currently logged in with devise
[rails] How to configure routing in resources
[Java] How to get to the front of a specific string using the String class
How to get the date in java
[rails] How to create a partial template
How to implement ranking functionality in Rails
How to change the value of a variable at a breakpoint in intelliJ
[For Rails beginners] Summary of how to use RSpec (get an overview)
How to publish a library in jCenter
How to use credentials.yml.enc introduced in Rails 5.2
Let's get rid of Devise's sickness and lead a comfortable Rails life!
[Rails] How to solve the time lag of created_at after save method
How to display error messages in Japanese