Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails Flash.now not working

I have a view from which I make an ajax request to the controller and after the action is successfully completed I initialize the flash.now[:notice]. But after the control goes back to the view. I don't happen to see the flash message.

flash.now[:notice] = "Request Completed successfully" if @meetings.any?
like image 990
Manjunath Manoharan Avatar asked Aug 20 '11 17:08

Manjunath Manoharan


4 Answers

When redirecting use

flash[:notice] = "This message value is available in next request-response cycle"

When rendering use

flash.now[:notice] = "Message is available in same request-response cycle"

Info from here

like image 136
AJP Avatar answered Oct 18 '22 21:10

AJP


Do you flash.now BEFORE you call render? Otherwise your message won´t appear.

like image 39
Arne Cordes Avatar answered Oct 18 '22 22:10

Arne Cordes


code in the controller:

flash[:success] = "All good!"
format.html { redirect_to some_path}

and in the view with close button:

<% flash.each do |key, value| %>
 <%= content_tag(:div, class: "alert alert-#{key} alert-dismissable") do %>
  <%= value %>
  <button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button>   
 <% end %> 
<% end %>
like image 3
fmnoise Avatar answered Oct 18 '22 22:10

fmnoise


Check you've got something like

<% flash.each do |key, value| %>
    <div class="flash <%= key %>"><%= value %></div>
<% end %>

in your application.html.erb file: if you don't you must add it, as this is where the notice will be displayed.

like image 2
Andrea Avatar answered Oct 18 '22 20:10

Andrea