【问题标题】:Rails ActiveStorage direct uploads on localhostRails ActiveStorage 直接上传到 localhost
【发布时间】:2018-08-15 13:36:34
【问题描述】:

使用 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,或者其他东西(配置错误)
  • 正确。这是本地主机。我正在使用本地选项。不过,我也尝试过使用 S3,但我得到了相同的结果。
  • config.active_storage.service = :local in config/environments/development.rb 对吗?
  • 设置为:localconfig.active_storage.service = :local
  • 发生在我身上;可以有;别人可以帮我猜

标签: ruby-on-rails rails-activestorage


【解决方案1】:

我遇到了同样的错误消息(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 隐藏默认浏览器按钮。有关演示,请参阅 here

【讨论】:

    【解决方案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 这是正确的代码。您只需删除带有注释的行,它就会起作用。
    猜你喜欢
    • 1970-01-01
    • 2021-01-27
    • 1970-01-01
    • 2011-09-01
    • 2018-07-01
    • 2019-05-15
    • 2018-11-13
    • 1970-01-01
    相关资源
    最近更新 更多