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,590
  • 7
  • 38
  • 76
  • [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

8

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,985
  • 1
  • 20
  • 32
  • Most upvoted but I still can't get it. Can you provide a code snippet or expand a little bit more? This are my params: `{"sample_pack"=> {"user_id"=>"1", "name"=>"Hello World", "samples_attributes"=> {"0"=>{"audio"=>"#", "_destroy"=>"0", "id"=>"24"}, "1"=>{"audio"=>"#", "_destroy"=>"0", "id"=>"25"}, "2"=>{"audio"=>"#", "_destroy"=>"0", "id"=>"26"}}}, "commit"=>"Update Sample pack", "id"=>"25"}` – Andres Urdaneta Mar 12 '23 at 16:23
  • Your `samples_attributes[0][:audio]` is passed in as a string (and that string is `""`). Same as the others. If this param is coming from the view (and debugged from the controller), then that's definitely the wrong thing to expect. Have you tried the other answer where you pass in `multipart: true` in `form_for` in the view? – Rystraum Mar 15 '23 at 09:09
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