使用 flash :error、:alert 和 :notice 有什么区别? [英] What is the difference when using flash :error, :alert, and :notice?

查看:68
本文介绍了使用 flash :error、:alert 和 :notice 有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

正如问题的标题所问,我很想知道在使用flash[:error]flash[:alert]flash[:alert]时是否有任何区别flash[:notice].如果是这样,什么时候使用它们是合适的,为什么?

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[:notice]. If so, when is it appropriate to use each, and why?

推荐答案

flash 是一种 Rails 机制,可以跨 2 个请求保存一些信息.您在一个请求中设置了 flash hash 中的某些内容,并且在您从同一客户端收到的下一个请求中可以使用它.

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.

由于 flash 只是一个哈希",因此您可以像使用它一样使用它.这意味着您可以提供您喜欢的键 (:alert/:error/:notice) 并将您希望的任何消息字符串作为值.

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.

什么或何时使用 :alert/:error/:notice 的语义完全由您来管理.话虽如此,一般的最佳实践是使用 :notice 当事情正常并以绿色显示时使用 :error 当事情不正常并以红色显示时使用 :error .如果您想将 :alert 用于 Web 应用程序上的另一种类型的消息,这完全没问题.我之前用过它的淡黄色调,仅供参考.

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.

这篇关于使用 flash :error、:alert 和 :notice 有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆