首页 > 解决方案 > 将 rails 4.2.8 升级到 5.0.x

问题描述

我正在将我的旧 Rails 应用程序升级rails 4.2.8rails 5.0.x(试用过的 rails 5.0.1 和 5.0.7 版本并遇到同样的问题)。

基础 Rails 应用程序升级顺利,我的基础 Rails 应用程序运行良好。但是,有一个内置 Angular 的前端应用程序和一个用 nodejs 构建的前端应用程序,它们具有一些通信通道,如 api 和一些控制器来与基本 Rails 应用程序通信。

我在其中一个在线控制器中遇到问题:

  render(:json =>  result, methods: [:mdobjectsAttributes, :blockAttributes, :entityFormTemplateBlock], :status => 200, :errorCode => 0, :layout => false)

错误是:

NoMethodError (undefined method `mdobjectsAttributes' for #Formtemplateblock:0x000055d7ff2a6480>):

所以,我知道我需要更新/更改 attr_accessible 和 attr_protected 功能以支持强参数。我在模型中看到了它们,但我不确定这是否会产生影响response methods……我确实更新了模型以ApplicationRecord代替 使用ActiveRecord::Base

另外,我确实更新jbuilder到了最新版本2.11.x

那么,对于解决方案有什么建议吗?显然我错过了什么?

在此处输入图像描述

在此处输入图像描述

在此处输入图像描述

这是显示错误的模型和控制器的预览。

标签: ruby-on-railsjsonrubyrenderupgrade

解决方案


在调查问题 2-3 天后,我设法通过将缺少的方法添加到所需模型作为 attr_accessor 来修复它,如下所示:

attr_accessor  :blockAttributes, :entityFormTemplateBlock...

因为我在 15 个模型中遇到了问题,所以我不得不通过创建 ruby​​ 模块来优化我的修复并将其包含到所需的模型中。

模块示例rails app/lib/custom_attr_accessors.rb

module CustomAttrAccessors
    attr_accessor  :blockAttributes, :entityFormTemplateBlock..
end

最后将该模块包含到模型中:

class Formtemplate < ApplicationRecord
  include CustomAttrAccessors
.....

推荐阅读