Rails Flash.now not working

后端 未结 5 882
-上瘾入骨i
-上瘾入骨i 2020-12-23 16:56

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 af

相关标签:
5条回答
  • 2020-12-23 17:42

    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.

    0 讨论(0)
  • 2020-12-23 17:53

    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

    0 讨论(0)
  • 2020-12-23 17:54

    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 %>
    
    0 讨论(0)
  • 2020-12-23 17:55

    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 %>
    
    0 讨论(0)
  • 2020-12-23 17:57

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

    0 讨论(0)
提交回复
热议问题