rails 4 — flash notice

前端 未结 3 553
别那么骄傲
别那么骄傲 2020-12-08 14:39

I\'m still working on my rails 4 demo site, and I\'m seeing an odd thing. In the controller it has a line like this:

format.html { redirect_to @widget, notic         


        
相关标签:
3条回答
  • 2020-12-08 15:15

    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.

    0 讨论(0)
  • 2020-12-08 15:20

    If you don't want to mess up with your ApplicationController as @Sachin Mour indicated, you can just add few addtional CSS clases, accordingly:

    in app/assets/stylesheets/custom.scss:

    /*flash*/
    .alert-error {
        background-color: #f2dede;
        border-color: #eed3d7;
        color: #b94a48;
        text-align: left;
     }
    
    .alert-alert {
        background-color: #f2dede;
        border-color: #eed3d7;
        color: #b94a48;
        text-align: left;
     }
    
    .alert-success {
        background-color: #dff0d8;
        border-color: #d6e9c6;
        color: #468847;
        text-align: left;
     }
    
    .alert-notice {
        background-color: #dff0d8;
        border-color: #d6e9c6;
        color: #468847;
        text-align: left;
     }
    

    Step by step tutorial, how to approach flash messages with devise and bootstrap, you can find here

    0 讨论(0)
  • 2020-12-08 15:35

    Add this to

    app/controllers/application_controller.rb

    class ApplicationController
      add_flash_types :success, :warning, :danger, :info
    end
    

    and then you can do this in your controllers

    format.html { redirect_to @widget, success: 'Widget was successfully created.' }
    

    provided you did this in your layouts

    <div class="container">
      <% flash.each do |key, value| %>
        <div class="alert alert-<%= key %>"><%= value %></div>
      <% end %>
      <%= yield %>
    </div>
    
    0 讨论(0)
提交回复
热议问题