代码之家  ›  专栏  ›  技术社区  ›  Joshua Pinter

Rails:一次添加多个flash[:notice]的简单方法

  •  36
  • Joshua Pinter  · 技术社区  · 16 年前

    flash[:notice]="Message" 它会将它添加到数组中,然后在查看过程中显示,但下面只保留最后一个闪光:

    flash[:notice] = "Message 1"
    flash[:notice] = "Message 2"
    

    现在我意识到这只是一个简单的带键散列(我想:)),但是有没有比下面更好的方法来进行多次闪烁:

    flash[:notice] = "Message 1<br />"
    flash[:notice] << "Message 2"
    
    8 回复  |  直到 10 年前
        1
  •  48
  •   mipadi    16 年前

    这个 flash

    flash[:notice] = ["Message 1"]
    flash[:notice] << "Message 2"
    

    然后在视图中,将其输出为

    <%= flash[:notice].join("<br>") %>
    

    或者随便你喜欢什么。

    更容易的 而其他解决方案则取决于您自己的喜好。

        2
  •  58
  •   Victor Savkin    16 年前

    我通常向ApplicationHelper添加以下方法:

    def flash_message(type, text)
        flash[type] ||= []
        flash[type] << text
    end
    

    以及

    def render_flash
      rendered = []
      flash.each do |type, messages|
        messages.each do |m|
          rendered << render(:partial => 'partials/flash', :locals => {:type => type, :message => m}) unless m.blank?
        end
      end
      rendered.join('<br/>')
    end
    

    在使用起来非常简单之后:

    flash_message :notice, 'text1'
    flash_message :notice, 'text2'
    flash_message :error, 'text3'
    

    在你的控制器里。

    只需在布局中添加以下行:

    <%= render_flash %>
    
        3
  •  5
  •   Jonathan Camenisch    15 年前

    我认为框架中的想法是,你在flash中粘贴的每一条消息都是过度可写的。为每条消息提供一个唯一的密钥,以便可以更改或覆盖它。

    如果这对你不起作用,考虑你是否真的需要简化你的UI。

        4
  •  2
  •   MunkiPhD    13 年前

    尽管我同意Jonathan的观点,UI可能需要一些简化,但在某些情况下,您可能希望为同一个键显示多条消息。

    github: flash-dance

        5
  •  1
  •   sekrett    11 年前

    在Rails 4.1上,这将不起作用。我有一个不同的实现,所以我认为您的代码应该这样更改:

    def render_flash
      rendered = []
      flash.keys do |type|
        flash[type].each do |m|
          rendered << render(:partial => 'partials/flash', :locals => {:type => type, :message => m}) unless m.blank?
        end
      end
      rendered.join('<br/>')
    end
    
        6
  •  0
  •   Yo Ludke    11 年前

    关于如何对换行的消息使用“传统”单行消息和数组样式的方法。

    使用Rails4.1和JSON序列化程序,可以使用一行flash消息

    flash[:notice] = 'message'
    

    和多行快闪信息

    flash[:notice]=“消息行1”,“消息行2”]

    像这样 application.html.haml 或相应的模板文件)

    - if notice
       #css_class 
       = safe_join(Array(notice), '<br/>'.html_safe)
    
        7
  •  0
  •   Ali Hassan Mirza    10 年前

    如果要在应用程序中添加flash消息,只需执行以下步骤。

    添加\u flash \u messages.html.erb文件并在其中添加此代码。

    <% flash.each do |type, message| %>
      <div class="alert <%= bootstrap_class_for(type) %> fade in">
        <button class="close" data-dismiss="alert">×</button>
        <%= message %>
      </div>
    <% end %>
    

    现在在application.html.erb中调用它,就像在application.html.erb中添加这一行一样

    <%= render partial: "shared/flash_messages", flash: flash %> 
    

    现在在application.helper.rb中添加此代码

    模块ApplicationHelper

    def bootstrap_class_for flash_type
      case flash_type
        when :success
          "alert-success"
        when :error
          "alert-error"
        when :alert
          "alert-block"
        when :notice
          "alert-info"
        else
          flash_type.to_s
      end
    end
    

    结束

    希望这对你有用。

        8
  •  0
  •   srghma    7 年前

    .container.mt-3.full-max-width-down-md
      - flash.each do |key, value|
        div{class="alert alert-#{key} alert-dismissible fade show text-center" role="alert"}
          button type="button" class="close" data-dismiss="alert" aria-label="Close"
            i class="fa fa-times" aria-hidden="true"
    
          - if value.is_a? Array
            = safe_join(value, "<br />".html_safe)
          - else
            = value
    

    例子

    flash[:notice] = "Hey"
    flash[:notice] = ["Hey", "Hey"]
    redirect_to my_path, flash: { notice: "Hey" }
    redirect_to my_path, flash: { notice: ["Hey", "Hey"] }
    
        9
  •  0
  •   cdmo    6 年前

    简单的方法来做多个闪光消息在一个时间没有太多的僵化,使用 this answer 作为灵感:

    flash[:notice] = "#{flash[:notice]} Message 1<br />"
    flash[:notice] = "#{flash[:notice]} Message 2"
    

    或

    flash[:notice] = flash[:notice].to_s + 'Message 1<br />'
    flash[:notice] = flash[:notice].to_s + 'Message 2'
    

    引用上述参考答案:

    这是因为NilClass定义了一个返回零长度字符串的#to#s方法,并且实例变量被自动初始化为nil。

    根据具体情况,尽管不是在这个问题的上下文中,您可能需要处理一个尾随的break标记或逗号或任何您正在使用的“delimeter”。你可以用很多方法 chomp