gpt4 book ai didi

ruby-on-rails - Rails ActiveStorage 错误 - MessageVerifier-InvalidSignature

转载 作者:数据小太阳 更新时间:2023-10-29 07:04:59 25 4
gpt4 key购买 nike

我正在开展一个项目,该项目需要在 Location 模型上使用 ActiveStorage has_many_attached :photos 情况。

我在下面设置了代码,但是在尝试上传表单时,我收到以下错误:

ActiveSupport::MessageVerifier::InvalidSignature in 
LocationsController#attach_photo

这是将文件“添加”到特定父记录(即 Location 记录)的附件集中的方法吗?

位置模型

class Location < ApplicationRecord
...
has_many_attached :photos
...
end

位置 Controller

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"

form_tag 默认不负责,必须有(如果附加文件)。

multipart/form-data No characters are encoded. This value is required when you are using forms that have a file upload control

enter image description here

表格:

<%= 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 方法,我们正在更新而不是创建 Idempotency

resources :locations do
member do
put :attach_photo
end
end

关于ruby-on-rails - Rails ActiveStorage 错误 - MessageVerifier-InvalidSignature,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50483901/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com