:alert :error :notice and :success?

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!


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:

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


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.

链接地址: http://www.djcxy.com/p/67798.html

上一篇: 导轨:防止闪烁消息显示两次

下一篇: :警告:错误:通知和:成功?