【问题标题】:How to add a notice/alert to a before_filter如何向 before_filter 添加通知/警报
【发布时间】:2015-04-07 10:58:34
【问题描述】:

所以我在使用 devise 时有一个如下过滤器:

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

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

我怎样才能将消息或警报添加到仅在存在重定向时才显示的前置过滤器...

类似:

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

【问题讨论】:

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


    【解决方案1】:

    尝试使用

    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

    【讨论】:

      【解决方案2】:

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

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

      【讨论】:

      • 这看起来很优雅,但我无法让它工作 - 任何指针?
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-09-05
      • 2023-04-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多