Rails ActiveStorage本地直传上传

3
使用Rails 5.2 rc1,在开发环境中是否可以在本地直接上传文件?
这是我的表单new.html.erb:
<div class="form-group">
  <%= f.label :images %>
  <%= f.file_field :images, multiple: true, direct_upload: true, class: 'form-control' %>
</div>

这是我在控制台中得到的错误:

ActiveSupport::MessageVerifier::InvalidSignature (ActiveSupport::MessageVerifier::InvalidSignature):

你正在使用特定的存储吗?看起来像是S3,或者其他(配置错误)。 - Ben
没错,这是本地主机。我正在使用本地选项。但我也尝试过使用 S3,但结果相同。 - stratis
“config.active_storage.service = :local” 在 “config/environments/development.rb” 文件中,对吗? - Ben
很遗憾,它什么也没做... - stratis
1
发生在我身上;本来可以的;我猜可能有其他人可以帮忙。 - Ben
显示剩余2条评论
2个回答

3
我遇到了相同的错误消息(rails 5.2.1)。问题是我的浏览器只是将文件名作为字符串提交,而不是上传文件。这发生的原因是我试图向文件输入添加一个 `style="display: none;"` 属性,这在尝试为上传按钮设置样式时很常见。不确定为什么浏览器(Chrome)会表现出这种方式。解决方案是不使用 `display:none`,而是使用 CSS 的 `opacity=0` 样式来设置按钮样式。不要这样做:
<label class="btn btn-primary btn-file">
    Custom element to upload an image, hurray!
    <%= f.file_field :avatar, accept: "image/*", style: "display: none;" %>
</label>

不要使用默认浏览器按钮,而是使用CSS来隐藏它。点击这里查看演示。


2
任何未来遇到这个问题的人:
我在我的控制器中错误地使用了这个:
def create
  @post = Post.new(post_params)
  @post.images.attach(post_params[:images]) # THIS WAS THE ERROR!!!

  respond_to do |format|
    if @post.save
      format.html { redirect_to @post, notice: 'Post was successfully created.' }
      format.json { render :show, status: :created, location: @post }
    else
      format.html { render :new }
      format.json { render json: @post.errors, status: :unprocessable_entity }
    end
  end
end

创建新文章并且附加已经存在的图片会导致各种错误,例如损坏的PG密钥和无效的签名。

确保在上传文件到新创建的模型实例时省略attach


你只是指出了错误的代码,那正确的代码在哪里呢? - chrisgeeq
1
@chrisgeeq 这是正确的代码。你只需要删除有注释的那一行,它就可以工作了。 - stratis

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接