首页 > 解决方案 > 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!

标签: ruby-on-railsrubyactioncontroller

解决方案


推荐阅读