ruby-on-rails - 将 rails 4.2.8 升级到 5.0.x
问题描述
我正在将我的旧 Rails 应用程序升级rails 4.2.8
到rails 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
。
那么,对于解决方案有什么建议吗?显然我错过了什么?
这是显示错误的模型和控制器的预览。
解决方案
在调查问题 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
.....
推荐阅读
- spring-boot - 无法反序列化 ExecutionContext:具有 [... Class] 且名称为 [... Class] 的类不受信任
- javascript - 如何在 React 功能组件中使用 Splide 同步方法
- python - 将 LATEX 中的量子力学方程加载到 Sympy 中?
- android - 我在 android studio 中渲染 SVG 文件时遇到问题
- vba - 如何显示加载项管理器对话框?
- java - 用 selenium 替换 http 客户端
- python - 在 AWS Lambda Python 脚本中转发自定义错误
- python - 使用 Amazon Alexa 加载 json 文件
- sql - SQLSERVER - 有没有办法强制每行只有一位列为真,其他所有列为假?
- node.js - 当我渲染一个我想创建作者的页面时,为什么要传递模型?