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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2448789/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 22:24:08  来源:igfitidea点击:

Rails: Easy way to add more than one flash[:notice] at a time

ruby-on-rails

提问by Joshua Pinter

I thought every time you do a flash[:notice]="Message"it would add it to the array which would then get displayed during the view but the following just keeps the last flash:

我想每次你做flash[:notice]="Message"它都会将它添加到数组中,然后在视图中显示,但以下只保留最后一个闪光:

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

Now I realize it's just a simple hash with a key (I think :)) but is there a better way to do multiple flashes than the following:

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

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

回答by Victor Savkin

I usually add such methods to my ApplicationHelper:

我通常将这样的方法添加到我的 ApplicationHelper 中:

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

And

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

And after it is very easy to use them:

之后就很容易使用它们了:

You can write something like:

你可以这样写:

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

in your controller.

在您的控制器中。

And just add this line to your layout:

只需将此行添加到您的布局中:

<%= render_flash %>

回答by mipadi

The flashmessage can really be anything you want, so you could do something like this:

flash消息实际上可以是您想要的任何内容,因此您可以执行以下操作:

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

And then in your views, output it as

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

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

Or whatever you prefer.

或者任何你喜欢的。

Whether that technique is easierthan other solutions is determined by your own preferences.

该技术是否比其他解决方案更容易取决于您自己的喜好。

回答by Jonathan Camenisch

I think the idea built into the framework is that every message you stick into flash is over-writeable. You give each message a unique key so you can change or overwrite it.

我认为框架中内置的想法是,您粘贴到闪存中的每条消息都是可重写的。您为每条消息指定一个唯一键,以便您可以更改或覆盖它。

If you need another message, don't call it ":notice." Call each something unique. Then to render the flash messages, loop through whatever is in the hash.

如果您需要另一条消息,请不要将其称为“:notice”。称每一个都是独一无二的。然后为了呈现 flash 消息,循环遍历哈希中的任何内容。

If this doesn't work for you, consider whether you actually need to simplify your UI.

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

回答by MunkiPhD

Although I agree with Jonathan in that the UI might need some simplification, there are instances where you might want to display multiple messages for the same key.

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

As a result, I have created a gem that (should) make it easy to deal with multiple flash messages in the same key and their rendering in the view.

因此,我创建了一个 gem,它(应该)可以轻松处理同一键中的多个 flash 消息及其在视图中的呈现。

GitHub: flash-dance

GitHub: 快闪舞

回答by sekrett

On Rails 4.1 this will not work. I have a different implementation, so I think your code should be changed this way:

在 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

回答by Yo Ludke

A method on how to use both 'traditional' one-liner messages and array-style for messages with line breaks.

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

With Rails 4.1 and the JSON serializer, you can use one-liner flash messages

使用 Rails 4.1 和 JSON 序列化程序,您可以使用单行 Flash 消息

flash[:notice] = 'message'

and multi-line flash messages

和多行快闪信息

flash[:notice] = ['message line 1', 'message line 2']

flash[:notice] = ['message line 1', 'message line 2']

like so (in application.html.hamlor the appropriate template file)

像这样(在application.html.haml或适当的模板文件中)

- if notice
   #css_class 
   = safe_join(Array(notice), '<br/>'.html_safe)

回答by Ali Hassan Mirza

If you want to add flash messages in your application you just need to flow these steps.

如果您想在您的应用程序中添加 Flash 消息,您只需执行这些步骤。

Add a _flash_messages.html.erb file and add this code in it.

添加 _flash_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 %>

And now call it in application.html.erb like add this line in application.html.erb

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

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

now add this code in application.helper.rb

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

module 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

end

结尾

This will work for you hopefully.

希望这对你有用。

回答by srghma

Bootstap 4, slim

Bootstap 4,纤薄

.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

Example

例子

flash[:notice] = "Hey"
flash[:notice] = ["Hey", "Hey"]
redirect_to my_path, flash: { notice: "Hey" }
redirect_to my_path, flash: { notice: ["Hey", "Hey"] }

回答by cdmo

Easy way to do multiple flash messages at a time without too much rigamarole, using this answeras inspiration:

使用此答案作为灵感,一次执行多条 Flash 消息的简单方法,而无需过多繁琐:

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

Or

或者

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

Quote from the above-referenced answer:

引用上面引用的答案:

This works because NilClass defines a #to_s method that returns a zero-length String, and because instance variables are automatically initialized to nil.

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

Depending on circumstances, though not in the context of this posted question, you might have to deal with a trailing break tag or comma or whatever "delimeter" you're using. Which you could deal with a number of ways like chomp

根据情况,尽管不在此发布问题的上下文中,您可能必须处理尾随中断标记或逗号或您正在使用的任何“分隔符”。您可以通过多种方式处理,例如chomp