rails 4 -- flash notice
In application.html.erb
, you would be displaying the flash
messages.
Update that code as below
<% flash.each do |name, msg| %>
<%= content_tag :div, msg, class: "alert alert-info" %>
<% end %>
You can add the classes that you want to apply to the flash message in the class
option.
EDIT
The class is setup as alert alert-notice
because of alert alert-<%= key %>
in your code.
When you call redirect_to @widget, notice: 'Widget was successfully created.
A flash message would be added in flash
hash with key as notice
and value as Widget was successfully created.
, i.e.,
flash[:notice] = "Widget was successfully created."
EDIT #2
format.html { redirect_to @widget, notice: 'Widget was successfully created.' }
notice: 'Widget was successfully created.'
is an argument passed to redirect_to
method. It is added to flash
hash in this method.
Rails 4- Flash messages are not showing
I got the same type of issue. When I checked the server log I got following error on the console
Can't verify CSRF token authenticity
For fixing this issue I have added following line the controller
protect_from_forgery with: :null_session
Ruby on rails flash notice error
Your controller code looks fine. I suspect your problem is not that you are calling the flash
method incorrectly in your controller, but rather that you did not write the code (correctly) to display the flash in your view.
flash
simply holds a message for your views to display. You are setting the message correctly, but you may not be displaying it properly. I can't tell because you aren't posting your view code.
For example, if this action is the result of submitting a user/login.html.erb
form, based on your controller code, you would want to have the following code inside that view file:
<% flash.each do |key, value| %>
<div class="alert"><%= value %></div>
<% end %>
This code is a more basic version of what is described in this article.
Rails 4 Do not work flash[:notice] and render
Try flash.now.notice = "Please chose the range between 09:00-17:00."
Related Topics
Failing Installing Pg Gem, "Mkmf.Rb Can't Find Header Files for Ruby" (MAC Osx 10.6.5)
How to Pass Data from a Controller to a Model with Ruby on Rails
How Is Each_With_Object Supposed to Work
Ruby: Split String at Character, Counting from the Right Side
Ruby 1.9: Regular Expressions with Unknown Input Encoding
What Does &: Mean in Ruby, Is It a Block Mixed with a Symbol
Is There a 'Pipe' Equivalent in Ruby
How to Enable Tls V1.2 in Ruby? If So, How
Why Doesn't "Rails S" Work from the App Directory
How to Get the Last SQL Query Performed by Activerecord in Ruby on Rails
Putting the Results of Pp (Or Anything Outputted to Console) into a String
301 Moved Permanently After S3 Uploading
How Is a Local Variable Created Even When If Condition Evaluates to False in Ruby