2011-09-28 44 views
25

在我的幾個控制器,我有重定向/閃存的消息ruby​​ on rails flash messages - :alert:error:notice and:success?

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

在我的會話控制器,然而,一旦認證成功,我有 閃光燈[:成功]「歡迎」 = redirect_to用戶

我希望能夠在我的其他控制器中做類似 :success =>「yay!」

這主要是爲了美觀/一致性的目的,但是:notice,:alert和:error唯一可用的閃存類型/是否可以添加其他類型?我有道理嗎?

謝謝!

回答

52

我相信沒有變化,這是接近你會得到:

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

下面是一些關於additional notes友好閃光燈語法加法。

9

我剛剛發現,在導軌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/register-your-own-flash-types-rails-4-countdown-to-2013

0

如果要訪問不同類型閃存的消息有關樣式的基礎引導警報(成功,警告),在你的控制器:

flash[:success] = "This works!" 

而且在佈局(最有可能application.html.erb)

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

與警告和其他引導警報樣式相同的東西。