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 ~
I tried it on Twitter to find a concrete example.
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.
Let's implement this flash.
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
As a test, let's press send without entering any value. It is validated and the error message is stored in flash [: notice]
.
Next, enter at least 20 characters and press send. The following error message is displayed:
Now let's try if the post is successful. It will be as follows.
It went well.
That's all for this article.
Thank you for your relationship.
Recommended Posts