Ruby-on-rails 如何使用 Twitter Bootstrap Rails gem 定义 Flash 通知

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

How to define Flash Notifications with Twitter Bootstrap Rails gem

ruby-on-railsruby-on-rails-3twitter-bootstrapflash-message

提问by barnett

I am trying to set flash notifications in the controller but it seems like I can only define :notice. When I try to define :errors or flash[:errors] I've gotten a bunch of errors. I currently have this working but all the messages are obviously a notice type and not an error or warning. I want to know how to set a error or warning instead of a notice. I am using rails 3.2 and the twitter bootstrap rails gem

我正在尝试在控制器中设置 Flash 通知,但似乎我只能定义 :notice。当我尝试定义 :errors 或 flash[:errors] 时,我遇到了一堆错误。我目前有这个工作,但所有消息显然都是通知类型,而不是错误或警告。我想知道如何设置错误或警告而不是通知。我正在使用 rails 3.2 和 twitter bootstrap rails gem

in application.html.erb

在 application.html.erb

<%= bootstrap_flash %>

in manuals_controller.rb

manuals_controller.rb

class ManualsController < ApplicationController
  def create
    respond_to do |format|
      format.html { redirect_to root_url, notice:'MyManual was successfully created.' }
    end
  end
end

回答by Daniel Kehoe

You're using the flash helper from seyhunak's twitter-bootstrap-railsgem. Instead of using the helper, you can set up the code yourself and see how everything works.

您正在使用 seyhunak 的twitter-bootstrap-railsgem 中的 flash 助手。您可以自己设置代码并查看一切如何工作,而不是使用帮助程序。

Here's how I set up Rails flash messages with Twitter Boostrap.

下面是我如何使用 Twitter Boostrap 设置 Rails 闪存消息。

Rails uses :notice and :alert as flash message keys. Twitter Bootstrap provides a base class .alert with additional classes .alert-success and .alert-error (see the Bootstrap documentation on alerts). A bit of parsing is required to get a Rails “notice” message to be styled with the Twitter Bootstrap “alert-success” style. Any other message, including a Rails “alert” message, will be styled with the Twitter Bootstrap “alert-error” style.

Rails 使用 :notice 和 :alert 作为 flash 消息键。Twitter Bootstrap 提供了一个基类 .alert 和附加类 .alert-success 和 .alert-error(请参阅有关警报的 Bootstrap 文档)。需要进行一些解析才能获得使用 Twitter Bootstrap“alert-success”样式设置样式的 Rails“通知”消息。任何其他消息,包括 Rails 的“警报”消息,都将使用 Twitter Bootstrap 的“警报错误”样式。

By default, Twitter Bootstrap applies a green background to .alert-success and a red background to .alert-error. Twitter Bootstrap provides a third class .alert-info with a blue background. With a little hacking, it's possible to create a Rails flash message with a custom name, such as :info, that will display with the Bootstrap .alert-info class. However, it's wise to stick with the Rails convention of using only “alert” and “notice.” Earlier versions of Rails used "error" but the current practice is to use "alert" instead of "error."

默认情况下,Twitter Bootstrap 对 .alert-success 应用绿色背景,对 .alert-error 应用红色背景。Twitter Bootstrap 提供了一个蓝色背景的第三类 .alert-info。稍加修改,就可以创建一个带有自定义名称的 Rails flash 消息,例如 :info,它将与 Bootstrap .alert-info 类一起显示。然而,坚持只使用“alert”和“notice”的 Rails 约定是明智的。早期版本的 Rails 使用“error”,但当前的做法是使用“alert”而不是“error”。

You can include code to display flash messages directly in your application layout file or you can create a partial. Here's an example with a partial.

您可以在应用程序布局文件中包含直接显示 Flash 消息的代码,也可以创建一个部分。这是一个带有部分的示例。

First, what goes in the application layout:

首先,应用程序布局中有什么:

# app/views/layouts/application.html.erb 
.
.
.
<%= render 'layouts/messages' %>
.
.
.

Next, the partial that gets included in the application layout:

接下来,包含在应用程序布局中的部分:

# app/views/layouts/_messages.html.erb
# Rails flash messages styled for Bootstrap 3.0
# works with Rails 4.0 or Rails 4.1
<% flash.each do |name, msg| %>
  <% if msg.is_a?(String) %>
    <div class="alert alert-<%= name.to_s == 'notice' ? 'success' : 'danger' %>">
      <button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button>
      <%= content_tag :div, msg, :id => "flash_#{name}" %>
    </div>
  <% end %>
<% end %>

And an example of setting two different flash messages in a controller:

以及在控制器中设置两个不同的 flash 消息的示例:

class VisitorsController < ApplicationController

  def new
    flash[:notice] = 'Welcome!'
    flash[:alert] = 'My birthday is soon.'
  end

end

This example comes from an in-depth article I wrote:

这个例子来自我写的一篇深入的文章:

Twitter Bootstrap and Rails

Twitter Bootstrap 和 Rails

For an alternative that accommodates four different flash message types (success, error, alert, notice), see an example of Rails Flash Messages using Twitter Bootstrap.

有关容纳四种不同 Flash 消息类型(成功、错误、警报、通知)的替代方案,请参阅使用 Twitter BootstrapRails Flash 消息示例。