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

前端 未结 5 1813
旧巷少年郎
旧巷少年郎 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: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 %>
    

提交回复
热议问题