Rails ActiveStorage错误 – MessageVerifier-InvalidSignature
我正在开发一个需要ActiveStorage
has_many_attached :photos
的项目has_many_attached :photos
Location
模型上的has_many_attached :photos
情况。
我在下面设置了代码,但在尝试上传表单时,收到以下错误:
ActiveSupport::MessageVerifier::InvalidSignature in LocationsController#attach_photo
这是将文件“添加”到特定父记录的附件集(即: Location
记录)的方法吗?
Location
模型
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
视图
视图
resources :locations do member do post :attach_photo end end
确保在form_tag
添加multipart: true
。 它生成enctype="multipart/form-data"
。
form_tag
默认不负责,必须拥有它(如果附加文件)。
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
to put
方法,我们正在更新而不是创建Idempotency
resources :locations do member do put :attach_photo end end
您需要将签名(在params[:signed_blob_id]
)分配给实例,如文档中的示例所示。
所以,像这样:
@location.photos.attach(params[:signed_blob_id]) # Signed reference to blob from direct upload