Rails 4 -- Flash Notice

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



Leave a reply



Submit