Explanation of Ruby on rails for beginners ⑦ ~ Flash implementation ~

Introduction

This time is a continuation of the previous article.

Please see the previous article if you like.

This time I will summarize about flash.

Explanation of Ruby on rails for beginners ①

Explanation of Ruby on rails for beginners ② ~ Creating links ~

Explanation of Ruby on rails for beginners ③ ~ Creating a database ~

Explanation of Ruby on rails for beginners ④ ~ How to use naming convention and form_Tag ~

Explanation of Ruby on rails for beginners ⑤ ~ Edit and delete database ~

Explanation of Ruby on rails for beginners ⑥ ~ Creation of validation ~

What is a flash?

I tried it on Twitter to find a concrete example.

image.png

The part where the entered user name is ~ is the flash.

The flash is displayed only once on the page, and when you refresh the page or move to another page, the flash disappears.

image.png

Let's implement this flash.

Flash implementation

A special variable, flash, is provided to display flash in rails.

You can use it in your view file by assigning a string to the variable flash [: notice] in the action. The flash is supposed to be automatically deleted once it has been used.

Also, since flash is commonly used in many places, it is convenient to use it in the ʻapplication.html.erb` file.

What is written in the ʻapplication.html.erb` file will be displayed in common to all view files.

Let's write the following in the application.html.erb file under the views >> layouts folder so that the flash can be displayed.

application.html.erb


<% if flash[:notice]%>
  <div class="flash">
    <%= flash[:notice]%>
  </div>
<% end %>

Now you can see the flash if it exists.

Anyway, let's set css as well.

application.css


.flash {
    background-color: brown;
    color: white;
}

Let's rewrite the posts controller as follows and assign the error message to flash [: notice].

posts_controller.rb


def create
  post = Post.new(content: params[:content])
  @content = params[:content]
  if post.save
    flash[:notice] = "Posted successfully"
    redirect_to("/posts/all")
  else
    flash[:notice] = post.errors.full_messages[0]
    render("posts/new")
  end
end

If the post.save part fails, the error messages are stored as a list in post.errors.full_messages, so the first value is stored in flash [: notice]. .. If the post is successful, store that fact in flash [: notice].

You have now implemented the flash.

Let's try it out.

Let's open the following new.html.erb file. In addition, the validation is as follows.

new.html.erb


<%= form_tag("/posts/create") do  %>  
  <textarea name="content" cols="30" rows="10"><%= @content%></textarea>
  <input type="submit" value="Send">
<% end %>

post.rb


class Post < ApplicationRecord
    validates :content, {presence: true}
    validates :content, {length: {maximum: 20}}
end

image.png

As a test, let's press send without entering any value. It is validated and the error message is stored in flash [: notice].

image.png

Next, enter at least 20 characters and press send. The following error message is displayed:

image.png

Now let's try if the post is successful. It will be as follows.

image.png

It went well.

At the end

That's all for this article.

Thank you for your relationship.

Recommended Posts

Explanation of Ruby on rails for beginners ⑦ ~ Flash implementation ~
Explanation of Ruby on rails for beginners ①
Explanation of Ruby on rails for beginners ② ~ Creating links ~
Explanation of Ruby on rails for beginners ③ ~ Creating a database ~
Explanation of Ruby on rails for beginners ⑤ ~ Edit and delete database ~
Ruby on Rails for beginners! !! Summary of new posting functions
[Procedure 1 for beginners] Ruby on Rails: Construction of development environment
[Ruby on Rails] About bundler (for beginners)
[Ruby on rails] Implementation of like function
Explanation of Ruby on rails for beginners ④ ~ Naming convention and how to use form_Tag ~
Implementation of Ruby on Rails login function (Session)
Ruby on Rails <2021> Implementation of simple login function (form_with)
Implementation of Ruby on Rails login function (devise edition)
Basic knowledge of Ruby on Rails
[Ruby on Rails] Comment function implementation
[Ruby on Rails] Introduction of initial data
Let's summarize "MVC" of Ruby on Rails
part of the syntax of ruby ​​on rails
Ruby on Rails for beginners! !! Post list / detailed display function summary
[Ruby on Rails] Follow function implementation: Bidirectional
[Ruby on Rails] Japanese notation of errors
How to build a Ruby on Rails environment using Docker (for Docker beginners)
[Ruby] Explanation for beginners of iterative processing with subscripts such as each_with_index!
Validation settings for Ruby on Rails login function
[Ruby on Rails] Until the introduction of RSpec
Recommendation of Service class in Ruby on Rails
[Ruby on Rails] Select2 introduction memo for Webpacker
Ruby on Rails ~ Basics of MVC and Router ~
[Ruby on Rails] A memorandum of layout templates
Ruby on Rails Email automatic sending function implementation
[Rails] Procedure for linking databases with Ruby On Rails
Ruby on Rails address automatic input implementation method
[Ruby on Rails] Individual display of error messages
Implementation of flash messages
Ruby on Rails Elementary
Ruby on Rails basics
Ruby On Rails Association
Scraping for beginners (Ruby)
[Ruby on Rails] Asynchronous communication of posting function, ajax
Docker the development environment of Ruby on Rails project
Try using the query attribute of Ruby on Rails
[Ruby on Rails] Implementation of validation that works only when the conditions are met
Rails implementation of ajax removal
Ruby on rails learning record -2020.10.03
Portfolio creation Ruby on Rails
Ruby on rails learning record -2020.10.04
Ruby on rails learning record -2020.10.09
Ruby on Rails config configuration
Ruby on Rails basic learning ①
[Rails 6] Implementation of search function
[Ruby on Rails] about has_secure_password
Ruby on rails learning record-2020.10.07 ②
[Rails] Implementation of category function
(For beginners) [Rails] Install Devise
Commentary on partial! --Ruby on Rails
Ruby on rails learning record-2020.10.07 ①
Cancel Ruby on Rails migration
[Rails] Implementation of tutorial function
Ruby on rails learning record -2020.10.06
[Rails] Implementation of like function
Ruby on Rails validation summary