rails 4 -- 闪现通知 [英] rails 4 -- flash notice

查看:28
本文介绍了rails 4 -- 闪现通知的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我仍在开发我的 rails 4 演示站点,我看到了一件奇怪的事情.在控制器中有这样一行:

format.html { redirect_to @widget,注意:'小部件已成功创建.'}

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

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

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

我的 Michael Hartl 启发了 layouts/application.html.erb:

<% flash.each do |key, value|%><div class="alert alert-<%= key %>"><%= value %></div><%结束%><%=产率%>

谢谢!

编辑 2:

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

解决方案

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

更新代码如下

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

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

编辑

该类设置为 alert alert-notice,因为您的代码中存在 alert alert-<%= key %>.当您调用 redirect_to @widget 时,请注意:'Widget 已成功创建.

将在 flash 哈希中添加一条 flash 消息,key 为 notice,值为 Widget 已成功创建.,即,>

flash[:notice] = "小部件已成功创建."

编辑 #2

format.html { redirect_to @widget,注意:'小部件已成功创建.'}

注意:'Widget 已成功创建.' 是传递给 redirect_to 方法的参数.在这个方法中它被添加到 flash 哈希中.

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:

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-notice rather than a valid Bootstrap alert class, like alert-info.

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

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?

EDIT: my Michael Hartl inspired 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:

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.html block. 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?

解决方案

In application.html.erb, you would be displaying the flash messages.

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 class option.

EDIT

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

A flash message would be added in flash hash with key as notice and value as Widget was successfully created., i.e.,

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

EDIT #2

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

notice: 'Widget was successfully created.' is an argument passed to redirect_to method. It is added to flash hash in this method.

这篇关于rails 4 -- 闪现通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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