2017-06-06 83 views
0

在Rails 4應用程序中,增加單個閃存[:錯誤]消息顯示持續時間的最簡單/最好的方法是什麼?Rails增加閃存[:錯誤]消息的持續時間

,做消息的代碼是在一個控制器和相關的部分看起來像這樣:

else 
    flash[:notice] = t(:no_new_media, source: @channel.external_source.capitalize) 
    flash[:error] = @error['message'] if @error.present? 
    redirect_to admin_channel_path(@channel) 
    end 

我想增加錯誤信息的顯示時間,但我不在乎無論哪種方式關於通知

+0

https://stackoverflow.com/questions/21467157/how-do-i-dismiss-a-rails-flash-message-after-some-duration – Pavan

回答

0

我認爲你應該創建一個新的自定義Flash消息類型。更多詳情here

# app/controllers/application_controller.rb 
class ApplicationController 
    ... 
    add_flash_types :error, :another_custom_type 
end 

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

現在在您的視圖中檢查flash[:another_custom_type].present?並在此基礎上設置消息的持續時間。

0

我不知道,但你可以嘗試這樣的: reference

$('document').ready(function() { 
    setTimeout(function() { 
    $('#test').slideUp(); 
    }, 5000); 
}); 

我認爲有閃光燈消息#TEST,這將後5000毫秒隱藏的HTML元素的ID(5秒)。

OR

app/assets/javascripts/application.js.coffee 

$ -> 
    flashCallback = -> 
    $(".flash-message").fadeOut() 
    $(".flash-message").bind 'click', (ev) => 
    $(".flash-message").fadeOut() 
    setTimeout flashCallback, 3000 

希望這會幫助你。

+0

@Craig如果你滿意我的答案,然後使其正確和投票 –