1

I can't figure out the reason of the error I get when updating a model containing an attached image without changing the image:

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"}

The error I'm getting is as follows:

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

Here is the controller update action code:

def update
    if @post.update(post_params)
      render json: @post
    else
      respond_with_errors @post
    end
  end

Here is what PostSerializer looks like:

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

I'm using Rails 5.2.0 API. What am I missing ?

belgoros
  • 3,057
  • 3
  • 25
  • 61
  • [ActiveStorage docs](https://edgeapi.rubyonrails.org/classes/ActiveStorage/Attached/One.html#method-i-attach) say that I have to get `params[:signed_blob_id]`. But how can I get it? I don't have it in my `params` hash: `params: {"data"=>{"id"=>"32", "attributes"=>{"title"=>"post-2", "body"=>"azertyuiop---dfdfdfd", "archived"=>true, "tag_ids"=>[13], "photo"=>"http://localhost:3000/rails/active_storage/blobs/bG9iX2lkIn19--6c69fa2c64924fadc07e5fb7989a4576bd897f74/diagram_events_13102018.png"}, "type"=>"posts"}, "controller"=>"posts", "action"=>"update", "id"=>"32"}` – belgoros Nov 06 '18 at 08:53

2 Answers2

5

The above error happens when you pass in a string when the system is expecting a File blob. You can work around the error by detecting if params[:photo] is a String or a File and only attaching if it was a File.

Rystraum
  • 1,885
  • 1
  • 20
  • 32
1

I had the exact same issue and it was fixed by adding enctype="multipart/form-data" to my form element.

the answer was found here https://stackoverflow.com/a/5628027/12910375

anthony
  • 104
  • 4