ruby on Rails 闪存消息 - :alert :error :notice 和 :success?

发布于 2024-12-06 14:50:49 字数 386 浏览 3 评论 0原文

在我的几个控制器中,我有重定向/闪现消息

redirect_to products_url, :notice => "message here", 
redirect_to states_url, :error => "oops!" etc... 

在我的会话控制器中,但是,在成功验证后,我有 flash[:成功] = "欢迎!" redirect_to user

我希望能够在我的其他控制器中执行类似的操作 :成功=> “耶!”

这主要是为了美观/一致性的目的,但是 :notice、:alert 和 :error 是唯一可用的 flash 类型/我可以添加其他类型吗?我说得有道理吗?

谢谢!

In several of my controllers, I have redirects/flash messages

redirect_to products_url, :notice => "message here", 
redirect_to states_url, :error => "oops!" etc... 

In my sessions controller, however, upon successful authentication, I have
flash[:success] = "welcome!"
redirect_to user

I'd like to be able in my other controllers to do something like
:success => "yay!"

This is mostly for cosmetic/consistency purposes, but are :notice, :alert and :error the only flash-types available / can I add additional types? Am I making sense?

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

辞取 2024-12-13 14:50:49

我相信,如果不进行更改,这将是您所得到的最接近的结果:

redirect_to user_path(@user), :flash => { :success => "Message" }

这里有一些 有关友好 Flash 语法添加的附加说明

I believe without changes, this is as close as you'll get:

redirect_to user_path(@user), :flash => { :success => "Message" }

Here's some additional notes regarding the friendly flash syntax addition.

猫弦 2024-12-13 14:50:49

我刚刚发现在 Rails 4 中你可以在应用程序控制器中注册自定义类型:

class ApplicationController
    ...
  add_flash_types :error, :another_custom_type
end

# app/controllers/users_controller.rb
class UsersController < ApplicationController
  def create
    ...
    redirect_to home_path,
      error: "An error message for the user"
  end
end

# app/views/home/index
<%= error %>

优点是 http://blog.remarkablelabs.com/2012/ 12/注册您自己的 flash-types-rails-4-2013 年倒计时

I just found out that in Rails 4 you can register custom types in app controller:

class ApplicationController
    ...
  add_flash_types :error, :another_custom_type
end

# app/controllers/users_controller.rb
class UsersController < ApplicationController
  def create
    ...
    redirect_to home_path,
      error: "An error message for the user"
  end
end

# app/views/home/index
<%= error %>

The merit goes to http://blog.remarkablelabs.com/2012/12/register-your-own-flash-types-rails-4-countdown-to-2013

神妖 2024-12-13 14:50:49

如果您想基于引导警报(成功和警告)访问不同类型的闪存消息样式,请在控制器中:

flash[:success] = "This works!"

在布局中(最有可能是 application.html.erb)

  <% if success.present? %>
      <p class="alert alert-success"><%= success %></p>
  <% end %>

与警告和其他引导警报样式相同。

If you want to access different types of flash messages styles based on bootstrap alert (success and warning), in you controller:

flash[:success] = "This works!"

And in your layout (most probably application.html.erb)

  <% if success.present? %>
      <p class="alert alert-success"><%= success %></p>
  <% end %>

Same thing with warning and other bootstrap alert styles.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文