Rails 6 ActiveStorage附加文件网址不起作用

问题描述

我有一个应用程序,用户可以在其中上载PDF文档,当上载pdf文档时(通过使用ActiveStorage DirectUpload的前端Stimulus控制器),然后处理PDF以将内容剥离到Excel电子表格中。一切都上传到s3存储桶。

我的模型如下:

class Export < ApplicationRecord
  has_one_attached :pdf
  has_one_attached :spreadsheet

  validates :pdf,attached: true,content_type: 'application/pdf'

  def process_pdf!
    self.pdf.open(tmpdir: "#{Rails.root}/tmp") do |file|
      tmpfile = File.open(file.path)
      file_name = File.basename(file.path).sub(".pdf","")
      op = PdfScraper.call(pdf_file: tmpfile,output_name: file_name)
      attach_spreadsheet(file_name: file_name)
    end
  end

  private

  def attach_spreadsheet(file_name:)
    self.spreadsheet.attach(
      io: File.open("#{Rails.root}/tmp/#{file_name}.xlsx"),filename: "#{file_name}.xlsx",identify: false,content_type: "application/vnd.ms-excel"
    )
  end
end

控制器如下:

class ExportsController < ApplicationController
  def create
    @export = Export.new(export_params)
    @export.uuid = cookies[:visitor_uuid]
    respond_to do |format|
      if @export.save
        @export.process_pdf!
        flash[:notice] = "Success!"
        format.html { redirect_to download_path }
      else
        flash[:error] = @export.errors
        format.html { redirect_to root_path }
      end
    end
  end

  private

  def export_params
    params.require(:export).permit(:pdf)
  end
end

重定向发生回到download_path时,我发现基于cookie ID的导出,但是当我尝试检查(或查看)附件电子表格的下载URL时,出现以下错误

undefined method `persisted?' for nil:NilClass

视图如下:

<p>
  <%= polymorphic_url(@export.spreadsheet) %>
</p>

我什至尝试了rails_blob_url(@export.spreadsheet),但是它不起作用。如果我通过Rails控制台查找URL,它似乎可以工作。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...