如何使用 Twitter Bootstrap Rails gem 定义 Flash 通知 [英] How to define Flash Notifications with Twitter Bootstrap Rails gem

查看:75
本文介绍了如何使用 Twitter Bootstrap Rails gem 定义 Flash 通知的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

在 application.html.erb 中

<%= bootstrap_flash %>

manuals_controller.rb

class ManualsController <应用控制器定义创建response_to do |格式|format.html { redirect_to root_url,注意:'MyManual 已成功创建.'}结尾结尾结尾

解决方案

您正在使用 seyhunak 的 twitter-bootstrap-rails gem.您可以自己设置代码,看看一切如何运作,而不是使用帮助程序.

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

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

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

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

首先,应用程序布局中的内容:

# app/views/layouts/application.html.erb...<%= 渲染布局/消息"%>...

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

# app/views/layouts/_messages.html.erb# 为 Bootstrap 3.0 设计的 Rails flash 消息# 适用于 Rails 4.0 或 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}" %>

<%结束%><%结束%>

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

class 访客控制器 <应用控制器定义新flash[:notice] = '欢迎光临!'flash[:alert] = '我的生日快到了.'结尾结尾

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

Twitter Bootstrap 和 Rails

对于适应四种不同的 flash 消息类型(成功、错误、警报、通知)的替代方案,请参阅 的示例使用 Twitter Bootstrap 的 Rails Flash 消息.

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

in application.html.erb

<%= bootstrap_flash %>

in 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

解决方案

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

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

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.

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."

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.

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:

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

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

这篇关于如何使用 Twitter Bootstrap Rails gem 定义 Flash 通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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