37

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?
Simone Carletti
  • 173,507
  • 49
  • 363
  • 364
Manjunath Manoharan
  • 4,567
  • 6
  • 28
  • 43

5 Answers5

105

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

AJP
  • 26,547
  • 23
  • 88
  • 127
4

If you use form_with you need to use local: true

<%= form_with @user, local: true do |f| %>
  <%= f.label :name %>
  <%= f.text_field :name %>
  <%= f.submit %>
<% end %>
mahfuz
  • 2,728
  • 20
  • 18
4

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

Arne Cordes
  • 581
  • 1
  • 6
  • 22
3

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 %>
Jerry Sha
  • 3,931
  • 2
  • 23
  • 16
fmnoise
  • 109
  • 4
2

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.

Andrea
  • 420
  • 2
  • 8