问题描述
我正在处理一个需要 ActiveStorage
has_many_attached :photos
情况在 Location
模型上的项目.
I'm working on a project that requires an ActiveStorage
has_many_attached :photos
situation on a Location
model.
我在下面设置了代码,但是在尝试上传表单时,我收到以下错误:
I have the code set up below, but when attempting to upload a form, I receive the following error:
ActiveSupport::MessageVerifier::InvalidSignature in
LocationsController#attach_photo
这是将文件添加"到特定父记录(即:Location
记录)的附件集中的方式吗?
Is this the way to "add" a file to the set of attachments for a particular parent record (i.e: a Location
record)?
class Location < ApplicationRecord
...
has_many_attached :photos
...
end
位置控制器
class LocationsController < ApplicationController
...
def attach_photo
@location = Location.find(params[:id])
@location.photos.attach(params[:photo])
redirect_to location_path(@location)
end
...
end
查看
<%= form_tag attach_photo_location_path(@location) do %>
<%= label_tag :photo %>
<%= file_field_tag :photo %>
<%= submit_tag "Upload" %>
<% end %>
查看
resources :locations do
member do
post :attach_photo
end
end
推荐答案
确保在 form_tag
中添加 multipart: true
.它生成 enctype="multipart/form-data"
.
Make sure to add multipart: true
in form_tag
. It generates enctype="multipart/form-data"
.
form_tag
默认不负责,必须有(如果附加文件).
form_tag
by default not responsible for it, must have it (if attaching a file).
multipart/form-data 没有编码字符.此值是必需的当您使用具有文件上传控件的表单时
表格:
<%= form_tag attach_photo_location_path(@location), method: :put, multipart: true do %>
<%= label_tag :photo %>
<%= file_field_tag :photo %>
<%= submit_tag "Upload" %>
<% end %>
还有:
将 post
更改为 put
方法,我们正在更新而不是创建 幂等性
Change post
to put
method, We are updating not creating Idempotency
resources :locations do
member do
put :attach_photo
end
end
这篇关于Rails ActiveStorage 错误 - MessageVerifier-InvalidSignature的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!