更新具有相同图像的模型时 ActiveSupport::MessageVerifier::InvalidSignature 错误

bel*_*ros 1 ruby-on-rails rails-api

在不更改图像的情况下更新包含附加图像的模型时,我无法弄清楚出现错误的原因:

Processing by PostsController#update as JSONAPI
  Parameters: {"data"=>{"id"=>"28", "attributes"=>{"title"=>"post-1", "body"=>"azertyui", "archived"=>true, "tag_ids"=>[11, 12, 13], "photo"=>"http://localhost:3000/rails/active_storage/blobs/eyJfcmFpbHMiOnsibWVzc2FnZSI6IkJBaHBFZz09IiwiZXhwIjpudWxsLCJwdXIiOiJibG9iX2lkIn19--86e7464f36c2eddf776573af7b9e61d969287158/diagram.png"}, "type"=>"posts"}, "id"=>"28"}
Run Code Online (Sandbox Code Playgroud)

我得到的错误如下:

Completed 500 Internal Server Error in 5ms (ActiveRecord: 0.5ms)
ActiveSupport::MessageVerifier::InvalidSignature (ActiveSupport::MessageVerifier::InvalidSignature):
app/controllers/posts_controller.rb:30:in `update'
Run Code Online (Sandbox Code Playgroud)

这是控制器更新操作代码:

def update
    if @post.update(post_params)
      render json: @post
    else
      respond_with_errors @post
    end
  end
Run Code Online (Sandbox Code Playgroud)

这是PostSerializer看起来的样子:

class PostSerializer < ActiveModel::Serializer
  include Rails.application.routes.url_helpers

  attributes :id, :title, :body, :tag_ids, :archived, :photo

  def photo
    rails_blob_url(object.photo) if object.photo.attached?
  end
end
Run Code Online (Sandbox Code Playgroud)

我正在使用 Rails 5.2.0 API。我错过了什么?

Rys*_*aum 5

当系统期望文件 blob 时传入字符串时,会发生上述错误。您可以通过检测params[:photo]是字符串还是文件并仅在它是文件时附加来解决该错误。