Flash in Rails

Flash contains different data after each HTTP request is sent
Temporary data in Flash disappear after each request
flash[:errors] = "Wrong username or password!"
flash.alert = "You must be a member to access this page!"flash.notice = "You are logged in" 
flash.keep             # keeps the entire flash
flash.keep(:alert) # keeps only the "notice" entry
flash.now[:message] = "Hello current action"
#app/controllers/signups_controller.rbdef create
@signup = Signup.create(signup_params)
flash[:notice] = "welcome message"
redirect_to user_path(@signup.user)
end
#app/views/signups/new.html.erb<p><%= flash[:notice] %></p>
  1. Set validations in the model. For this example I have set validations in the “signup” model in order to check on the presence of “name” to be true and “age” to be equal or greater than 18 upon creating new signups;
#app/models/signup.rbvalidates :name, presence: true
validates :age, numericality: {greater_than_or_equal_to: 18}
#app/controllers/signups_controller.rbdef create 
@signup = Signup.create(signup_params)
if @signup.valid?
redirect_to user_path(@signup.user)
else
flash[:my_errors] = @signup.errors.full_messages
redirect_to new_signup_path
end
end
#app/views/signups/new.html.erb<% if flash[:my_errors] %>   <% flash[:my_errors].each do |error| %>   <p><%= error %></p><% end %><%= form_for @signup do |f| %>   <p><%= f.label :name %>
<%= f.text_field :name %></p>
<p><%= f.label :age %>
<%= f.number_field :age %></p>
<p><%= f.submit %><% end %>

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store