【问题标题】:Rails Wicked_PDF ActiveStorageRails Wicked_PDF ActiveStorage
【发布时间】:2020-04-04 23:48:32
【问题描述】:

我正在使用 crono gem 将我的 Rails 6 应用程序中的 PDF 导出到 Cronjob 中。这本身工作正常,我的样式应用于表格等:

def perform
        view = ActionController::Base.new
        view.extend(ApplicationHelper)
        view.extend(Rails.application.routes.url_helpers)
        # include helpers and routes
        view.instance_variable_set("@clinic_image", Configuration.first.image)
        view.instance_variable_set("@meal_triplet_week_vollkost", Meal.this_week_vollkost)
        view.instance_variable_set("@meal_triplet_week_schonkost", Meal.this_week_schonkost)
        view.instance_variable_set("@meal_triplet_week_vegetarisch", Meal.this_week_vegetarisch)
        view.instance_variable_set("@clinic_name", Configuration.first.clinicName)

        pdf = WickedPdf.new.pdf_from_string(
            view.render_to_string('meals/exportMealsCurrentWeek.html.erb', layout: 'pdf_week.html.erb'))

        save_path = Rails.root.join('pdfs','filename.pdf')
        File.open(save_path, 'wb') do |file|
            file << pdf
        end
    end

我的问题如下:

当我调用从控制器渲染 PDF 的操作时,我的数据库中的 ActiveStorage 图像会毫无问题地显示出来:

<%= image_tag(@clinic_image, size: "400x64", alt: "BDH Klinik Braunfels") %>

@clinic_image 是从我的数据库中在应用程序的默认内容中定义的:

if Configuration.first.image.attached?
   @clinic_image = Configuration.first.image 
end

但在 cronjob 中生成的 PDF 中,出现以下两个错误:

如果我将 image_tag 保留在我得到的视图中:

无法将图像解析为 URL:#

的未定义方法 `polymorphic_url'

如果我尝试使用 wicked_pdf_image_tag 而不是普通的 image_tag 助手,我会收到以下错误:

没有将 ActiveStorage::Attached::One 隐式转换为字符串

所以我要么需要正确的 URL(我首先必须下载我认为的图像),要么需要 ActiveStorage 图像作为字符串。

我从 GitHub 存储库尝试了一些“解决方案”或更好的解决方法,但它似乎对我不起作用(也许我做错了什么,不确定)。

如果我使用资产文件夹中图像的正常路径,它*可以工作,但我总是需要在配置中设置的图像。

有没有办法解决这个问题?

【问题讨论】:

    标签: ruby-on-rails ruby rails-activestorage wicked-pdf


    【解决方案1】:

    使用 wicked_pdf_image_tag 自己修复了它,并编写了一个辅助函数,将请求放入资产管道。

    【讨论】:

    • 干得好。或许你可以分享一下 Max 这个很棒的功能,好吗?
    • 插话过来问一下代码 Max
    猜你喜欢
    • 1970-01-01
    • 2019-05-19
    • 1970-01-01
    • 2011-07-22
    • 2011-12-16
    • 1970-01-01
    • 2019-04-15
    • 2018-11-02
    • 1970-01-01
    相关资源
    最近更新 更多