What is the difference when using flash :error, :alert, and :notice?

前端 未结 5 1787
旧巷少年郎
旧巷少年郎 2020-12-25 11:03

As the title of the question asks, I am interested in knowing if there is any difference when using flash[:error], flash[:alert], and flash[:

相关标签:
5条回答
  • 2020-12-25 11:37

    this is just a classification. it generates the div #error.error or div#notice.notice you connect the logic you want above

    litte sample :

    .alert, .error, .notice, .success { padding:.8em 0; margin:0 0 2px 0; border:2px solid #ffffd; font-size:1.6em; text-align:center;}
    
    .error { background:#FBE3E4;color:#8a1f11;border-color:#FBC2C4; }
    .notice { background:#FFF6BF;color:#514721;border-color:#FFD324; }
    .success { background:#DDFCD5;color:#000;border-color:#44A815; }
    .alert { background:#FBE3E4;color:#8a1f11;border-color:#FBC2C4; }
    
    .error a {color:#8a1f11;}
    .notice a {color:#514721;}
    .success a {color:#264409;}
    .alert a {color:#8a1f11;}
    
    0 讨论(0)
  • 2020-12-25 11:40

    flash is a Rails mechanism to persist some information across 2 requests. You set something in flash hash in one request and it's available in the very next request you receive from that same client.

    Since flash is just a "hash" you can use it like one. Which means you can provide your preferred key (:alert/:error/:notice) and put whatever message string you wish as the value.

    The semantics of what or when to use :alert/:error/:notice are really up to you to manage. Having said that, the general best practice is to use :notice when things are OKAY and is displayed in a greenish hue, and use :error when things are NOT OKAY and is displayed in a reddish hue. It is completely okay if you want to use :alert for another type of message on your web app. I've used it before for informational purposes in a yellowish hue.

    0 讨论(0)
  • 2020-12-25 11:40

    They are just different classifications. I mainly use :error and :notice. Notice I use for informational messages ("your password has been changed", "changes saved", etc). I reserve :error for critical things/problems ("Your passwords do not match", "Login failed", etc)

    0 讨论(0)
  • 2020-12-25 11:50

    :alert and :notice functionally differ from other keys you invent. FlashHash provides convenience accessors for the two: flash.alert , flash.notice. Rails' preference for these two further rears its way into redirect_to which will only accept :alert, :notice, or :flash.

    However, a commit in July 2012 to edge Rails allows the privilege of adding other flash types. Here's an example of adding custom flash types in Rails 4:

    # app/controllers/application_controller.rb
    class ApplicationController; add_flash_types(:error, :annoyance); end
    
    # app/controllers/monopoly_controller.rb
    class MonopolyController < ApplicationController
      def chance
        ...
        redirect_to haha_path, annoyance: "Go directly to jail. Do not pass Go. Do not collect $200."
      end
    end
    
    # app/views/haha/index.html.erb
    <%= annoyance %>
    
    0 讨论(0)
  • 2020-12-25 11:57

    I also use a :message class too pass along the StandardError message trapped in my exception handlers. Using 2 or 3 classes allows you to display up to that many messages in response to a single event or result, each conveying a different aspect of the outcome, e.g. informational, an error based on what the application knows and an error based on what the system knows.

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