Ruby-on-rails Rails 中有哪些 Flash 消息类型可用?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9063751/
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-03 02:43:13  来源:igfitidea点击:

What flash message types are available in Rails?

ruby-on-rails

提问by cjm2671

So far I've got:

到目前为止,我有:

  • :notice
  • :alert
  • :error
  • :注意
  • :警报
  • :错误

but is there definitive list, that can be used in place, such as in redirect_to path, :error => "Oh no!"?

但是是否有明确的列表,可以就地使用,例如在redirect_to path, :error => "Oh no!"

采纳答案by Hauleth

No, as a flash type you can use any symbol, even your own.

不,作为一种闪光类型,您可以使用任何符号,甚至是您自己的符号。

回答by Steve Grossi

Hauleth is correct that you can use any symbol, but right now, :noticeand :alertare the only ones you can pass directlyinto redirect_to(according to flash.rb in Rails source), as you specifically mention:

Hauleth是正确的,你可以使用任何的象征,但现在,:notice并且:alert是可以传递的唯一直接进入redirect_to(根据在Rails的源flash.rb),为您特别提到:

redirect_to path, :error => "Oh no!" # Will not work

If you want a different flash type such as :error(or :success), you must pass those in through the :flashkey, like so:

如果您想要不同的 flash 类型,例如:error(或:success),则必须通过:flash键传递它们,如下所示:

redirect_to path, :flash => { :error => "Oh no!" }

For information on how to register your custom flash types so that, like :noticeand :alert, you can pass them directly in to redirect_to, see this StackOverflow Q&A: https://stackoverflow.com/a/3848759/995663

有关如何注册自定义的闪存类型,这样,类似的信息:notice:alert,您可以直接通过他们redirect_to,看到这个StackOverflow的Q&A:https://stackoverflow.com/a/3848759/995663

Update:According to this commit, it seems Rails 4 will make this easier by allowing you to register custom flash types by calling add_flash_types :errorin ApplicationController.

更新:根据此提交,似乎 Rails 4 将允许您通过调用add_flash_types :errorApplicationController来注册自定义 Flash 类型,从而使这变得更容易。