Ruby-on-rails rails 4 -- 闪现通知

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

rails 4 -- flash notice

ruby-on-railsrubytwitter-bootstrapruby-on-rails-4

提问by user101289

I'm still working on my rails 4 demo site, and I'm seeing an odd thing. In the controller it has a line like this:

我还在我的 rails 4 演示站点上工作,我看到了一件奇怪的事情。在控制器中有这样一行:

format.html { redirect_to @widget, notice: 'Widget was successfully created.' }

This renders a flash message in the redirected page, which is expected. However, the css class attached to the message div is alert alert-noticerather than a valid Bootstrap alert class, like alert-info.

这会在重定向页面中呈现一条 Flash 消息,这是预期的。但是,附加到消息 div 的 css 类alert alert-notice不是有效的 Bootstrap 警报类,例如alert-info.

Where is the class being set for this flash, and how do I customize it?

为该 Flash 设置的类在哪里,我该如何自定义它?

Also, if I'm deleting a record via ajax, is there a way to access the core flash container to display the message via js, or do I have to show / hide my own flash message div just for ajax requests?

另外,如果我通过 ajax 删除记录,有没有办法访问核心 Flash 容器以通过 js 显示消息,或者我是否必须显示/隐藏我自己的 Flash 消息 div 仅用于 ajax 请求?

EDIT: my Michael Hartl inspired layouts/application.html.erb:

编辑:我的迈克尔哈特启发了layouts/application.html.erb

<div class="container">
  <% flash.each do |key, value| %>
    <div class="alert alert-<%= key %>"><%= value %></div>
  <% end %>
  <%= yield %>
</div>

Thanks!

谢谢!

EDIT 2:

编辑2:

Perhaps I wasn't clear enough in my original question. I understand exactly how the class is being set in the flash object in this case. I am interested in learning how to use and customize the notice:in the format.htmlblock. It seems there should be a way to pass a class via this notice? Or is this not a core Rails way of doing things?

也许我在最初的问题中不够清楚。我完全理解在这种情况下如何在 flash 对象中设置类。我感兴趣的学习如何使用和自定义notice:format.html块。似乎应该有一种方法可以通过此通知传递类?或者这不是 Rails 的核心做事方式?

回答by Kirti Thorat

In application.html.erb, you would be displaying the flashmessages.

在 中application.html.erb,您将显示flash消息。

Update that code as below

更新该代码如下

  <% flash.each do |name, msg| %>
    <%= content_tag :div, msg, class: "alert alert-info" %>
  <% end %>

You can add the classes that you want to apply to the flash message in the classoption.

您可以在class选项中添加要应用于 Flash 消息的类。

EDIT

编辑

The class is setup as alert alert-noticebecause of alert alert-<%= key %>in your code. When you call redirect_to @widget, notice: 'Widget was successfully created.

alert alert-notice由于alert alert-<%= key %>在您的代码中设置了该类。你打电话时redirect_to @widget, notice: 'Widget was successfully created.

A flash message would be added in flashhash with key as noticeand value as Widget was successfully created., i.e.,

将在flash哈希中添加一条闪存消息,键为notice,值为Widget was successfully created.,即,

flash[:notice] = "Widget was successfully created."

EDIT #2

编辑#2

format.html { redirect_to @widget, notice: 'Widget was successfully created.' }

format.html { redirect_to @widget, notice: 'Widget was successfully created.' }

notice: 'Widget was successfully created.'is an argument passed to redirect_tomethod. It is added to flashhash in this method.

notice: 'Widget was successfully created.'是传递给redirect_to方法的参数。flash在此方法中将其添加到哈希中。

回答by Sachin Mour

Add this to

将此添加到

app/controllers/application_controller.rb

应用程序/控制器/application_controller.rb

class ApplicationController
  add_flash_types :success, :warning, :danger, :info
end

and then you can do this in your controllers

然后你可以在你的控制器中做到这一点

format.html { redirect_to @widget, success: 'Widget was successfully created.' }

provided you did this in your layouts

如果您在布局中执行此操作

<div class="container">
  <% flash.each do |key, value| %>
    <div class="alert alert-<%= key %>"><%= value %></div>
  <% end %>
  <%= yield %>
</div>

回答by w1t3k

If you don't want to mess up with your ApplicationControlleras @Sachin Mour indicated, you can just add few addtional CSS clases, accordingly:

如果您不ApplicationController想像@Sachin Mour 指出的那样搞砸,您可以相应地添加一些额外的 CSS 类:

in app/assets/stylesheets/custom.scss:

app/assets/stylesheets/custom.scss

/*flash*/
.alert-error {
    background-color: #f2dede;
    border-color: #eed3d7;
    color: #b94a48;
    text-align: left;
 }

.alert-alert {
    background-color: #f2dede;
    border-color: #eed3d7;
    color: #b94a48;
    text-align: left;
 }

.alert-success {
    background-color: #dff0d8;
    border-color: #d6e9c6;
    color: #468847;
    text-align: left;
 }

.alert-notice {
    background-color: #dff0d8;
    border-color: #d6e9c6;
    color: #468847;
    text-align: left;
 }

Step by step tutorial, how to approach flash messages with devise and bootstrap, you can find here

分步教程,如何使用设计和引导程序处理 Flash 消息,您可以在此处找到