我有一个Ruby on Rails 7项目,其模型名为chapters.rb,它有两个活动存储附件:视频和封面图片。在我的表单中,我为每个文件都有一个文件字段,我已经仔细检查了所有内容的名称是否正确。问题是我只能上传一个视频(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
1条答案
按热度按时间tjrkku2a1#
为了将多个文件附加到ActiveStorage中的单个模型,我们可以使用has_many_attached。
举例来说:
has_many_attached :files
使用上面的一个代替has_one_attached