一种型号上有两个 Active Storage 附件 - 一个可以使用,另一个不能使用

问题描述 投票:0回答:1

我有一个 Ruby on Rails 7 项目,其模型名为chapters.rb,它有两个活动存储附件:视频和 cover_image。在我的表单中,每个文件都有一个文件字段,并且我已仔细检查所有内容是否命名正确。问题是我只能上传视频(Quicktime),而不能上传封面图片(jpg)。当我上传视频时,表单会提交并很快重定向到显示页面并按预期显示上传的视频。

但是,当我尝试上传 cover_image 时,出现一个对话框,其中显示错误“读取 [无论文件名是什么] 时出错”。在 javascript 控制台中,我收到此错误:无法加载资源:操作无法完成。 (WebKitBlobResource 错误 4。)。我的 Rails 控制台上没有显示任何内容。视频和 cover_image 似乎都配置正确并且类似。这绝对令人困惑。有什么想法吗?

相关代码如下:

  belongs_to :curriculum
  # has_one :curriculum_video, dependent: :destroy
  has_many :sections, dependent: :destroy
  has_one_attached :cover_image
  has_one_attached :video

end

<%= form_with(model: [@curriculum, @chapter], class: "contents") do |form| %>
  <% if @chapter.errors.any? %>
    <div id="error_explanation" class="bg-red-50 text-red-500 px-3 py-2 font-medium rounded-lg mt-3">
      <h2><%= pluralize(@chapter.errors.count, "error") %> prohibited this chapter from being saved:</h2>

      <ul>
        <% chapter.errors.each do |error| %>
          <li><%= error.full_message %></li>
        <% end %>
      </ul>
    </div>
  <% end %>


  <%= form.hidden_field :curriculum_id %>

  <div class="my-5">
    <%= form.label :title %>
    <%= form.text_field :title, class: "block shadow rounded-md border border-gray-200 outline-none px-3 py-2 mt-2 w-full" %>
  </div>

  <div class="my-5">
    <%= form.label :description %>
    <%= form.text_field :description, class: "block shadow rounded-md border border-gray-200 outline-none px-3 py-2 mt-2 w-full" %>
  </div>

  <div class="my-5">
    <%= form.label :chapter_video %>
    <%= form.file_field :video, direct_upload: true, class: "block shadow rounded-md border border-gray-200 outline-none px-3 py-2 mt-2 w-full"%>
  </div> 

  <div class="my-5">
    <%= form.label "Cover Image" %>
    <%= form.file_field :cover_image, direct_upload: true, class: "block shadow rounded-md border border-gray-200 outline-none px-3 py-2 mt-2 w-full"%>
  </div>
  


  <div class="inline">
    <%= form.submit class: "rounded-lg py-3 px-5 bg-blue-600 text-white inline-block font-medium cursor-pointer" %>
  </div>
<% end %>

<script>
document.querySelector('.contents').addEventListener('submit', (event) => {
  console.log('Form submitted!');
});

</script>
# Relevant chapters_controller.rb
# GET /chapters/1/edit
  def edit
    authorize @chapter
  end

  # POST /chapters or /chapters.json
  def create
    # @chapter = Chapter.new(chapter_params)
    @chapter = @curriculum.chapters.build(chapter_params)
    authorize @chapter

    respond_to do |format|
      if @chapter.save
        format.html { redirect_to curriculum_path(@curriculum), notice: "Chapter was successfully created." }
        format.json { render :show, status: :created, location: @chapter }
      else
        format.html { render :new, status: :unprocessable_entity }
        format.json { render json: @chapter.errors, status: :unprocessable_entity }
      end
    end
  end

  # PATCH/PUT /chapters/1 or /chapters/1.json
  def update
    authorize @chapter

    respond_to do |format|
      if @chapter.update(chapter_params)
        format.html { redirect_to curriculum_chapter_path(@curriculum, @chapter), notice: "Chapter was successfully updated." }
        format.json { render :show, status: :ok, location: @chapter }
      else
        format.html { render :edit, status: :unprocessable_entity }
        format.json { render json: @chapter.errors, status: :unprocessable_entity }
      end
    end
  end
ruby-on-rails rails-activestorage
1个回答
0
投票

为了将多个文件附加到 ActiveStorage 中的单个模型,我们可以使用 has_many_attached。
例如:

has_many_attached :files
使用上面的 has_one_attached

© www.soinside.com 2019 - 2024. All rights reserved.