NoMethodError: 未定义方法 `halt_callback_chains_on_return_false=' 用于 ActiveSupport:Module [英] NoMethodError: undefined method `halt_callback_chains_on_return_false=' for ActiveSupport:Module

查看:34
本文介绍了NoMethodError: 未定义方法 `halt_callback_chains_on_return_false=' 用于 ActiveSupport:Module的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开发这个 RoR 5.1 应用程序已经有一段时间了,现在我需要添加一个新的迁移:

I've been developing this RoR 5.1 application for a while, and I need to add a new migration now:

class AddActiveFlagToParameters < ActiveRecord::Migration[5.1]
  def change
    add_column :parameters, :is_active, :boolean, :default => true 
  end
end

当我尝试运行迁移时,rails 引发错误:

When I try to run the migration, rails raises the error:

NoMethodError: 未定义的方法`halt_callback_chains_on_return_false=' 用于 ActiveSupport:Module

NoMethodError: undefined method `halt_callback_chains_on_return_false=' for ActiveSupport:Module

阅读后,我终于通过升级到 Rails 5.2(gem activesupport 5.2.0)并注释掉文件 config/initializers/new_framework_defaults.rb 中的行来解决这个问题

Reading around, I finally worked around the issue by upgrading to Rails 5.2 (gem activesupport 5.2.0) and commenting out the line in the file config/initializers/new_framework_defaults.rb

但这听起来像是一个短期解决方案.

But this sounds like a short term solution.

这是从哪里来的?我怎样才能安全地处理这个问题?

Where does this come from? How can I safely handle this issue ?

推荐答案

halt_callback_chains_on_return_false 初始化程序中的设置是升级到 Rails 5.0 后临时保留旧回调行为的解决方案.假设您需要时间检查应用程序中的所有回调,然后您可以删除此设置.并假设在升级到 5.2 时已检查所有内容,因此删除此设置.

halt_callback_chains_on_return_false setting in the initializer was a solution for temporary keeping old callback behaviour after upgrade to Rails 5.0. Assumed that you need time to check all callbacks in the app and after it you can remove this setting. And assumed that on the upgrade to 5.2 all is already checked, so this setting is removed.

在 Rails 5 之前,从 ActiveModel 或 ActiveModel::Validations、ActiveRecord 和 ActiveSupport 中的任何 before_ 回调返回 false 导致回调链停止.

Before Rails 5, returning false from any before_ callback in ActiveModel or ActiveModel::Validations, ActiveRecord and ActiveSupport resulted in halting of callback chain.

从 Rails 5.0 开始,如果任何 before_ 回调返回 false,则回调链不会停止.要显式停止回调链,我们需要使用 throw(:abort).

Starting from Rails 5.0 if any before_ callback returns false then callback chain is not halted. To explicitly halt the callback chain, we need to use throw(:abort).

因此,您需要检查应用程序中的所有 before_callbacks 是否有正确的行为,如果需要进行更改,然后从初始化程序中删除此行.

So you need to check all before_callbacks in the app for proper behaviour, change them if needed and remove this line from initializer after it.

您可以阅读更多这里

这篇关于NoMethodError: 未定义方法 `halt_callback_chains_on_return_false=' 用于 ActiveSupport:Module的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆