2015-04-07 19 views
2

所以我使用設計的時候有一個前過濾器,如以下未登錄。但用戶可能會感到困惑,並且想知道爲什麼他們打算使用新操作時將其重定向到那裏。如何通知/警報添加到的before_filter

我怎麼能過濾器,顯示了只有在有重定向前添加一個消息或警報一...

喜歡的東西:

notice: 'You must sign in before creating a new product!' 

回答

0

我最終在測試不同的事情時找到了更好的解決方案。我覺得它更簡單,更直觀:

before_action :authenticate_user!, only: [:new, :destroy, :edit, :update], notice: 'you must sign in first!' 
+0

這看起來很優雅,但我不能得到它的工作 - 任何指針? –

0

Here is a short thread解決您的問題。

從螺紋:

before_filter [:authenticate]

def concatenate_flash(&block) 
    warning = flash[:warning].blank? ? [] : [ flash[:warning] ] 
    yield 
    warning << flash[:warning] unless warning.include? flash[:warning] 
    flash[:warning] = warning 
end 

def authenticate 
    concatenate_flash do 
    authenticate_user! 
    end 
end 

然後,在我的意見/佈局/ application.haml,我已經改變了我閃到:

- [:notice, :alert, :error, :success, :warning].each do |key| 
    - unless flash[key].blank? 
     - if flash[key].kind_of?(Array) and flash[key].flatten.compact.empty? 
      - next key 
     = render "shared/flash", key: key, flash: flash