Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails ActiveStorage Error - MessageVerifier-InvalidSignature

I'm working on a project that requires an ActiveStorage has_many_attached :photos situation on a Location model.

I have the code set up below, but when attempting to upload a form, I receive the following error:

ActiveSupport::MessageVerifier::InvalidSignature in 
                                 LocationsController#attach_photo

Is this the way to "add" a file to the set of attachments for a particular parent record (i.e: a Location record)?

Location Model

class Location < ApplicationRecord
  ...
  has_many_attached :photos
  ...
end

Locations Controller

class LocationsController < ApplicationController
  ...
  def attach_photo
    @location = Location.find(params[:id])
    @location.photos.attach(params[:photo])
    redirect_to location_path(@location)
  end
  ...
end

View

<%= form_tag attach_photo_location_path(@location) do %>
  <%= label_tag :photo %>
  <%= file_field_tag :photo %>

  <%= submit_tag "Upload" %>
<% end %>

View

resources :locations do
  member do
    post :attach_photo
  end
end
like image 726
slehmann36 Avatar asked May 23 '18 08:05

slehmann36


1 Answers

Make sure to add multipart: true in form_tag. It generates enctype="multipart/form-data".

form_tag by default not responsible for it, must have it (if attaching a file).

multipart/form-data No characters are encoded. This value is required when you are using forms that have a file upload control

enter image description here

Form:

<%= form_tag attach_photo_location_path(@location), method: :put, multipart: true do %>
  <%= label_tag :photo %>
  <%= file_field_tag :photo %>

  <%= submit_tag "Upload" %>
<% end %>

Also:

Change post to put method, We are updating not creating Idempotency

resources :locations do
  member do
    put :attach_photo
  end
end
like image 65
7urkm3n Avatar answered Dec 01 '22 21:12

7urkm3n