ActiveStorage::DirectUploadsController - Cannot POST /rails/active_storage/direct_uploads

I tried to use dire upload way from Ember app to Rails API and used ember-active-storage add-on. Rails responds with the following error when I tried to attach a file:

Cannot POST /rails/active_storage/direct_uploads

404 - Resource was not found.

All Rails controller are protected with before_action filter defined in ApplicationController:

class ApplicationController < ActionController::API

before_action :authorize_request

attr_reader :current_user

private

def authorize_request @current_user = (AuthorizeApiRequest.new(request.headers).call)[:user] end end

``

I inspected the headers used when posting the request, - it seems to be OK and contains Authorization:

Authorization:

Bearer eyJhbG…

and the request payload:

  1. blob: {filename: “Screenshot 2018-10-31 at 10.45.21.png”, content_type: “image/png”, byte_size: 110466,…}
  2. byte_size: 110466
  3. checksum: “L+1wS2rPR7cKkXP/Rb/rig==”
  4. content_type: “image/png”
  5. filename: “Screenshot 2018-10-31 at 10.45.21.png”

What’s wrong with that ? Thank you.

Here is what I found on the Net and it helped to move forward but there are still some errors:

  • I created DirectUploadsController by extending it from ActiveStorage::DirectUploadsController as follows: class DirectUploadsController < ActiveStorage::DirectUploadsController protect_from_forgery with: :exception skip_before_action :verify_authenticity_token end

``

I defined a new route corresponding to the post request to the above route:

Rails.application.routes.draw do resources :posts post ‘/rails/active_storage/direct_uploads’ => ‘direct_uploads#create’ end

``

I modified the Post serializer as follows:

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

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

def photo url_for(object.photo) if object.photo.attached? end end

``

Here is Post model:

class Post < ApplicationRecord serialize :tag_ids, Array validates :title, :body, :tag_ids, presence: true

has_one_attached :photo end

``

And finally PostsController has a new strong_parameter attribute - photo:

def create @post = Post.new(post_params)

if @post.save
  render json: @post, status: :created, location: @post
else
  respond_with_errors @post
end

end

def post_params ActiveModelSerializers::Deserialization.jsonapi_parse!(params, only: [:title, :body, :tag_ids, :archived, :photo]) end

``

But when posting a new Post data, there are still errors:

Started POST “/posts” for 127.0.0.1 at 2018-10-31 16:26:48 +0100

Processing by PostsController#create as JSONAPI

Parameters: {“data”=>{“attributes”=>{“title”=>“post-1”, “body”=>“azertyui”, “archived”=>true, “tag_ids”=>[12], “photo”=>“eyJfcmFpbHMiOnsibWVzc2FnZSI6IkJBaHBEZz09IiwiZXhwIjpudWxsLCJwdXIiOiJibG9iX2lkIn19–6209e9184fe8b05b5ce883ac1d38cfde6925c16b”}, “type”=>“posts”}}

ActiveStorage::Blob Load (0.2ms) SELECT “active_storage_blobs”.* FROM “active_storage_blobs” WHERE “active_storage_blobs”.“id” = ? LIMIT ? [[“id”, 9], [“LIMIT”, 1]]

↳ app/controllers/posts_controller.rb:18

(0.0ms) begin transaction

↳ app/controllers/posts_controller.rb:18

(0.0ms) commit transaction

↳ app/controllers/posts_controller.rb:18

(0.0ms) begin transaction

↳ app/controllers/posts_controller.rb:20

Post Create (0.3ms) INSERT INTO “posts” (“title”, “body”, “tag_ids”, “archived”, “created_at”, “updated_at”) VALUES (?, ?, ?, ?, ?, ?) [[“title”, “post-1”], [“body”, “azertyui”], [“tag_ids”, “—\n- 12\n”], [“archived”, 1], [“created_at”, “2018-10-31 15:26:49.023471”], [“updated_at”, “2018-10-31 15:26:49.023471”]]

↳ app/controllers/posts_controller.rb:20

ActiveStorage::Attachment Create (0.3ms) INSERT INTO “active_storage_attachments” (“name”, “record_type”, “record_id”, “blob_id”, “created_at”) VALUES (?, ?, ?, ?, ?) [[“name”, “photo”], [“record_type”, “Post”], [“record_id”, 28], [“blob_id”, 9], [“created_at”, “2018-10-31 15:26:49.025251”]]

↳ app/controllers/posts_controller.rb:20

Post Update (0.1ms) UPDATE “posts” SET “updated_at” = ? WHERE “posts”.“id” = ? [[“updated_at”, “2018-10-31 15:26:49.026607”], [“id”, 28]]

↳ app/controllers/posts_controller.rb:20

(1.4ms) commit transaction

↳ app/controllers/posts_controller.rb:20

(0.0ms) begin transaction

↳ app/controllers/posts_controller.rb:20

ActiveStorage::Blob Update (0.3ms) UPDATE “active_storage_blobs” SET “metadata” = ? WHERE “active_storage_blobs”.“id” = ? [[“metadata”, “{"identified":true}”], [“id”, 9]]

↳ app/controllers/posts_controller.rb:20

(0.6ms) commit transaction

↳ app/controllers/posts_controller.rb:20

[ActiveJob] Enqueued ActiveStorage::AnalyzeJob (Job ID: 09817808-2dcd-499e-ade3-cc9e7fa0156a) to Async(default) with arguments: #<GlobalID:0x00007f8b3aee0ee0 @uri=#<URI::GID gid://draft-api/ActiveStorage::Blob/9>>

ActiveStorage::Blob Load (0.1ms) SELECT “active_storage_blobs”.* FROM “active_storage_blobs” WHERE “active_storage_blobs”.“id” = ? LIMIT ? [[“id”, 9], [“LIMIT”, 1]]

↳ /Users/Serguei/.rvm/gems/ruby-2.5.0/gems/activerecord-5.2.0/lib/active_record/log_subscriber.rb:98

[ActiveJob] [ActiveStorage::AnalyzeJob] [09817808-2dcd-499e-ade3-cc9e7fa0156a] Performing ActiveStorage::AnalyzeJob (Job ID: 09817808-2dcd-499e-ade3-cc9e7fa0156a) from Async(default) with arguments: #<GlobalID:0x00007f8b38350370 @uri=#<URI::GID gid://draft-api/ActiveStorage::Blob/9>>

[ActiveJob] [ActiveStorage::AnalyzeJob] [09817808-2dcd-499e-ade3-cc9e7fa0156a] Skipping image analysis because the mini_magick gem isn’t installed

[ActiveJob] [ActiveStorage::AnalyzeJob] [09817808-2dcd-499e-ade3-cc9e7fa0156a] (0.0ms) begin transaction

[ActiveJob] [ActiveStorage::AnalyzeJob] [09817808-2dcd-499e-ade3-cc9e7fa0156a] ↳ /Users/Serguei/.rvm/gems/ruby-2.5.0/gems/activerecord-5.2.0/lib/active_record/log_subscriber.rb:98

[ActiveJob] [ActiveStorage::AnalyzeJob] [09817808-2dcd-499e-ade3-cc9e7fa0156a] ActiveStorage::Blob Update (0.2ms) UPDATE “active_storage_blobs” SET “metadata” = ? WHERE “active_storage_blobs”.“id” = ? [[“metadata”, “{"identified":true,"analyzed":true}”], [“id”, 9]]

[ActiveJob] [ActiveStorage::AnalyzeJob] [09817808-2dcd-499e-ade3-cc9e7fa0156a] ↳ /Users/Serguei/.rvm/gems/ruby-2.5.0/gems/activerecord-5.2.0/lib/active_record/log_subscriber.rb:98

[ActiveJob] [ActiveStorage::AnalyzeJob] [09817808-2dcd-499e-ade3-cc9e7fa0156a] (0.6ms) commit transaction

[ActiveJob] [ActiveStorage::AnalyzeJob] [09817808-2dcd-499e-ade3-cc9e7fa0156a] ↳ /Users/Serguei/.rvm/gems/ruby-2.5.0/gems/activerecord-5.2.0/lib/active_record/log_subscriber.rb:98

[ActiveJob] [ActiveStorage::AnalyzeJob] [09817808-2dcd-499e-ade3-cc9e7fa0156a] Performed ActiveStorage::AnalyzeJob (Job ID: 09817808-2dcd-499e-ade3-cc9e7fa0156a) from Async(default) in 5.75ms

[active_model_serializers] Rendered PostSerializer with ActiveModelSerializers::Adapter::JsonApi (1.67ms)

Completed 500 Internal Server Error in 71ms (ActiveRecord: 4.1ms)

ArgumentError (Missing host to link to! Please provide the :host parameter, set default_url_options[:host], or set :only_path to true):

Why so ! Where shoudl I declare hot to link to ? Or :host_parameter, etc ? Thank you