skip_before_action和Rails 5

Bit*_*ise 18 ruby rspec ruby-on-rails actioncontroller ruby-on-rails-5

我刚刚升级到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
Run Code Online (Sandbox Code Playgroud)

And*_*eko 26

根据这个帖子

ActiveSupport :: Callbacks#skip_callbacknow如果删除了无法识别的回调,则会引发ArgumentError.

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

skip_before_action :redirect_heroku_user, raise: false
Run Code Online (Sandbox Code Playgroud)

有关详细信息,请参阅更改日志.


dp7*_*dp7 6

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

您可以通过这里提到的raise: false避免它:

skip_before_action :redirect_heroku_user, only: :edit, raise: false
Run Code Online (Sandbox Code Playgroud)