如何向before_filter添加通知/警报

时间:2015-04-07 10:58:34

标签: ruby-on-rails ruby-on-rails-4 devise alert before-filter

因此,在使用设计时,我有一个像以下一样的过滤器:

before_action :authenticate_user!, only: [:new, :destroy, :edit, :update]

因此,每当我进入新操作时,如果我没有登录,我会被重定向到users_sign_in路由。但是,用户可能会感到困惑,并且想知道为什么他们在打算使用新操作时会将其重定向到那里。

如何向仅在有重定向的情况下显示的过滤器添加消息或提醒...

类似的东西:

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

2 个答案:

答案 0 :(得分:0)

尝试使用

flash[:notice] = 'You must sign in before creating a new product!'

http://api.rubyonrails.org/classes/ActionDispatch/Flash.html

请记住在布局文件中显示闪烁。 见http://guides.rubyonrails.org/action_controller_overview.html#the-flash

答案 1 :(得分:0)

我在测试不同的东西时最终找到了更好的解决方案。我认为它更简单,更直观:

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