简体   繁体   中英

Flash notice in Ruby on Rails

Sorry for the question, but I'm a real beginner in RoR here, so please bear with me as i'm taking a course but I'm not really getting what the course content creator explained.


In 1 of controllers, several actions have:

    flash[:notice]= "Article was successfully <some different text with each action>"


In app/views/layouts/_messages.html.erb

<% flash.each do |name, msg| %>
  <ul>
    <li><%= msg %></li>
  </ul>
<% end %>


In app/views/layouts/application.html.erb

<%= render 'layouts/messages' %>

Course creator explained that I need a loop as it's an array of arrays? How do I see that it is one from the codes? Thanks for the help and how does a loop work as it's only flash[name]= "message (like a hash) everytime? Thanks.

The flash provides a way to pass temporary primitive-types (String, Array, Hash) between actions. Anything you place in the flash will be exposed to the very next action and then cleared out. This is a great way of doing notices and alerts, such as a create action that sets flash[:notice] = "Post successfully created" before redirecting to a display action that can then expose the flash to its template. Actually, that exposure is automatically done.

<% flash.each do |name, msg| %>
  <ul>
    <li><%= msg %></li>
  </ul>
<% end %>

In above, name is basically a type of flash message. there are a different type of flash like notice, alert etc.

In this flash message

flash[:notice]= "Article was successfully <some different text with each action>"

notice is type of flash and "Artice was ...." is actually a message

Hash create in below format

{notice: "Article was successfully "}

message type is used for setting appropriate css for different message type . for an eg in alert flash we need to show message in red color, if it is success than we need to show message in green color.

if you want to check the each name and msg you can print that name and message with below code

<% flash.each do |name, msg| %>
  <% puts name %>
  <% puts msg %>
  <ul>
    <li><%= msg %></li>
  </ul>
<% end %>

If you want to more about flash messages please check this link

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM