(this article translated from: https://www.rubyguides.com/2019/11/rails-flash-messages/)

What is instant messaging?

Instant messaging is a way to display information to your Rails website users to tell them what’s going on.

Example:

  • Password changed (confirmation prompt)
  • User not found (error message)

Instant messages are usually set up in the Controller and rendered in the View. Your users get the information they need.

Let’s learn how it works.

How to use instant messaging

You can initiate prompt messages by using Flash helper functions.

They work a lot like Ruby Hash.

Instant Messaging objects have features like Keys, Any? “, or the each method, to access real-time information, uses [].

So, what types of messages can you set?

By default, you can set:

  • notice
  • alert

Here’s a use example:

flash.alert = "User not found."

Another style:

flash[:alert] = "User not found."

(They’re just different in style)

You can use this code in your Controller Actions like index, create, new and so on.

Another way to use it:

redirect_to :books_path, notice: "Book not found"

This allows you to redirect pages and create instant messages in one step.

This is great!

Contrast Alert Notice

As far as I know, alert or notice is not very important. Use whichever suits you.

I like to use alert to display error messages and notice to display prompt messages.

It’s mostly a difference of style.

For example

You can display alert as red and notice as green.

You can also use add_flash_types in the controller to create your own message types as needed.

Like this:

class ApplicationController
  add_flash_types :info, :error, :warning
end

I like to keep it simple, so the built-in message types are good enough for me.

Render instant messages

Instant messages don’t show up automatically, you have to render them on your view before you can see them. You might consider adding them to the layout view.

Here’s the code:

<% flash.each do |type, msg| %>
  <div>
    <%= msg %>
  </div>
<% end %>

This code is placed where you want to display it, usually at the top of the page, under the menu bar.

Remember:

Instant messages are displayed only once, then removed and never displayed again.

Beautify your instant messages

Instant messaging has no built-in style by default.

If you use Bootstrap, you can add the Alert Alert – Info CSS classes to your instant messages and they will look much better.

Example:

<% flash.each do |type, msg| %>
  <div class="alert alert-info">
    <%= msg %>
  </div>
<% end %>

If you did not use Bootstrap, you can customize the notification style according to your requirements.

When do I need to display instant messages?

Once the instant message is displayed, it will be removed the next time the controller takes action.

Based on this feature, make a summary:

  • If you callredirect_to“And shows thatflashInstant messages in the hash, then everything is fine
  • If you callredirect_to, but does not show the instant message, then the message is still saved inflashThe hash
  • If you’re setting messages toflashHash the same action to display instant messages, thenflashThe message will be displayed, butflashThe message in is not deleted, so it is displayed twice.

So…

How do I display instant messages in the current action?

Flash. now makes its debut.

Here’s an example:

def index
  @books = Book.all
  flash.now[:notice] = "We have exactly #{@books.size} books available."
end

This will render the instant message in the Index view.

Notice will be displayed and deleted immediately. In this way, the message will not be displayed twice.

So, when you want to use render instead of redirecting the page, you need to use Flash.Now for the message display.

conclusion

In this article, you learned about instant messaging in Rails and how to use it properly.

Finally, it’s important to note the difference between instant messaging and error validation. Error validation is related to model objects, and you need to use the errors method of the model to access information about validation errors, such as @user.errors.

Now it’s time to write some code to put what you’ve learned in this article into practice.

Thanks for reading.