【问题标题】:skip_before_action and Rails 5skip_before_action 和 Rails 5
【发布时间】:2017-01-31 12:14:33
【问题描述】:

我刚刚升级到 Rails 5,一切都很顺利,但没有明显原因,在 skip_before_action 之后调用的方法不允许 rspec 使用此消息运行

在 process_action 回调之前:redirect_heroku_user 尚未定义(ArgumentError)

这非常奇怪,因为它在 rails 4 上运行良好。这是我的代码:

# application_controller.rb
def redirect_heroku_user
  redirect_to root_path if heroku_user?
end 

# some_controller.rb
skip_before_action :redirect_heroku_user, only: :edit

【问题讨论】:

  • 你在其他地方有 before_action :redirect_heroku_user 吗?我认为实际的错误是告诉你不能跳过 before_action 因为 before_action 从未真正设置过。我可能错了,但是添加 rails: false 作为建议的答案感觉就像你不应该做的事情,除非你真的需要它来生产。

标签: ruby-on-rails ruby rspec ruby-on-rails-5 actioncontroller


【解决方案1】:

根据this thread

ActiveSupport::Callbacks#skip_callback now raises an ArgumentError if an unrecognized callback is removed.

所以您的解决方案是将raise: false 选项传递给skip_before_action

skip_before_action :redirect_heroku_user, raise: false

请参阅changelog 了解更多信息。

【讨论】:

    【解决方案2】:

    在 Rails 5 中,如果 redirect_heroku_user 方法没有在同一个控制器中定义,则会引发此异常。

    您可以将raise: false 传递为mentioned here 来避免它:

    skip_before_action :redirect_heroku_user, only: :edit, raise: false
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-05-26
      • 2018-03-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-05-27
      相关资源
      最近更新 更多