[英]ruby on rails flash messages - :alert :error :notice and :success?
In several of my controllers, I have redirects/flash messages 在我的几个控制器中,我有重定向/ flash消息
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!" 但是,在我的会话控制器中,成功验证后,我有flash [:success] =“welcome!” redirect_to user
redirect_to用户
I'd like to be able in my other controllers to do something like :success => "yay!" 我希望能够在我的其他控制器中做类似的事情: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! 谢谢!
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. 下面是一些附加说明关于友好闪光灯语法加法。
I just found out that in Rails 4 you can register custom types in app controller: 我刚刚发现在Rails 4中你可以在app控制器中注册自定义类型:
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 优点是http://blog.remarkablelabs.com/2012/12/register-your-own-flash-types-rails-4-countdown-to-2013
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) 并在您的布局(最可能是application.html.erb)
<% if success.present? %>
<p class="alert alert-success"><%= success %></p>
<% end %>
Same thing with warning and other bootstrap alert styles. 警告和其他引导警报样式也是如此。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.