mercredi 31 octobre 2018

Rails API - how to enable ActiveStorage::DirectUploadsController

I'm using Rails API (5.2.0) and hit a problem when uploading a file directly from an Ember app by posting to ActiveStorage::DirectUploadsController#create (/rails/active_storage/direct_uploads path). I'm getting 404error. Does it means that the above controller is just not availabe in Rails API-mode app ? If so, how is it possible to include the corresponding module ? Or I'll have to create a custom controller inheriting from ActiveStorage::DirectUploadsController?

I created DirectUploadsController extending ActiveStorage::DirectUploadsControlleras follows:

class DirectUploadsController < ActiveStorage::DirectUploadsController
  protect_from_forgery with: :exception
  skip_before_action :verify_authenticity_token
end

And defined Post model:

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

  has_one_attached :photo
end

PostsSerializer looks like that:

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

When checking if a photo was attached to the Post, it seems to be OK:

2.5.0 :001 > post = Post.first
  Post Load (0.1ms)  SELECT  "posts".* FROM "posts" ORDER BY "posts"."id" ASC LIMIT ?  [["LIMIT", 1]]
 => #<Post id: 28, title: "post-1", body: "azertyui", tag_ids: [12], archived: true, created_at: "2018-10-31 15:26:49", updated_at: "2018-10-31 15:26:49"> 
2.5.0 :002 > post.photo.attached?
  ActiveStorage::Attachment Load (0.2ms)  SELECT  "active_storage_attachments".* FROM "active_storage_attachments" WHERE "active_storage_attachments"."record_id" = ? AND "active_storage_attachments"."record_type" = ? AND "active_storage_attachments"."name" = ? LIMIT ?  [["record_id", 28], ["record_type", "Post"], ["name", "photo"], ["LIMIT", 1]]
  ActiveStorage::Blob Load (0.1ms)  SELECT  "active_storage_blobs".* FROM "active_storage_blobs" WHERE "active_storage_blobs"."id" = ? LIMIT ?  [["id", 9], ["LIMIT", 1]]
 => true 

But when trying to display it in the front-end app on posts/index page it fails with errors:

Started GET "/posts" for 127.0.0.1 at 2018-10-31 17:13:20 +0100
   (0.1ms)  SELECT "schema_migrations"."version" FROM "schema_migrations" ORDER BY "schema_migrations"."version" ASC
  ↳ /Users/Serguei/.rvm/gems/ruby-2.5.0/gems/activerecord-5.2.0/lib/active_record/log_subscriber.rb:98
Processing by PostsController#index as JSONAPI
  Post Load (0.1ms)  SELECT "posts".* FROM "posts"
  ↳ app/controllers/posts_controller.rb:8
[active_model_serializers]   ActiveStorage::Attachment Load (0.2ms)  SELECT  "active_storage_attachments".* FROM "active_storage_attachments" WHERE "active_storage_attachments"."record_id" = ? AND "active_storage_attachments"."record_type" = ? AND "active_storage_attachments"."name" = ? LIMIT ?  [["record_id", 28], ["record_type", "Post"], ["name", "photo"], ["LIMIT", 1]]
[active_model_serializers]   ↳ app/serializers/post_serializer.rb:7
[active_model_serializers]   ActiveStorage::Blob Load (0.1ms)  SELECT  "active_storage_blobs".* FROM "active_storage_blobs" WHERE "active_storage_blobs"."id" = ? LIMIT ?  [["id", 9], ["LIMIT", 1]]
[active_model_serializers]   ↳ app/serializers/post_serializer.rb:7
[active_model_serializers] Rendered ActiveModel::Serializer::CollectionSerializer with ActiveModelSerializers::Adapter::JsonApi (30.88ms)
Completed 500 Internal Server Error in 45ms (ActiveRecord: 2.2ms)



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

app/serializers/post_serializer.rb:7:in `photo'
app/controllers/posts_controller.rb:8:in `index'

Where should I set those options ? :host parameter, set default_url_options[:host], or set :only_path to true

I created two repos to reproduce the problem:

  1. Rails API app
  2. Ember App (front-end client)



Aucun commentaire:

Enregistrer un commentaire