ruby-on-rails - What to do about "is not an ActiveModel-compatible object. It must implement :to_partial_path." error when trying to use render_to_string in a PORO
问题描述
I refactored some of my code that generates a PDF into a separate class that lives in /lib. Problem is that the class depends on the render_to_string
method from ActionController::Base
.
The code is quite simple:
class Pdf
def pdf_content(record)
render_to_string(
partial: 'pdf/content.pdf.haml',
layout: 'pdf/application.pdf.erb',
locals: { record: record.decorate }
)
end
def render_to_string(*params)
ActionController::Base.new.render_to_string(params)
end
end
When I try to call the class Pdf.new.pdf_content(record)
I get an error: is not an ActiveModel-compatible object. It must implement :to_partial_path.
Thanks!
解决方案
推荐阅读
- javascript - JavaScript img.onload() 有时不会触发(取决于设备和文件大小)
- android - 使用辅助功能服务从屏幕上读取所有文本
- php - 如何从 Woocommerce 的“新订单电子邮件”中排除特定的客户详细信息属性
- angular - Angular2:如果值在整个表列中相同,则使用 PrimeNG 动态隐藏列
- java - 使用主题列表映射 KafkaStream
- c++ - 在 Mac M1 上找不到“GLFW/glfw3.h”文件
- ruby-on-rails - 初始页面加载时的 ActiveStorage::FileNotFoundError(有时)
- python-2.7 - vmcloak 初始化失败,挂载了 win7 iso
- node.js - 尝试使用 nodejs 从 Shopify 获取分析数据
- python - 在 Jupyter Notebboks 中保存绘图时出现驱动错误