Я использую Rails API (5.2.0
) и столкнулся с проблемой при загрузке файла непосредственно из приложения Ember, отправив сообщение в ActiveStorage :: DirectUploadsController # create (/rails/active_storage/direct_uploads
path). Я получаю 404
ошибку. Означает ли это, что вышеуказанный контроллер просто недоступен в приложении Rails API-mode? Если да, то как можно включить соответствующий модуль? Или мне придется создать собственный контроллер, наследующий от ActiveStorage::DirectUploadsController
?
Я создал DirectUploadsController
, расширяя ActiveStorage::DirectUploadsController
следующим образом:
class DirectUploadsController < ActiveStorage::DirectUploadsController
protect_from_forgery with: :exception
skip_before_action :verify_authenticity_token
end
И определенная модель поста:
class Post < ApplicationRecord
serialize :tag_ids, Array
validates :title, :body, :tag_ids, presence: true
has_one_attached :photo
end
PostsSerializer
выглядит так:
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
При проверке, была ли фотография прикреплена к Посту, вроде бы все в порядке:
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
Но при попытке отобразить его во внешнем приложении на странице posts/index
происходит сбой с ошибками:
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'
Где мне установить эти опции?
:host parameter, set default_url_options[:host], or set :only_path to true
Я создал два репозитория, чтобы воспроизвести проблему:
- Приложение Rails API
- Ember App (клиент переднего плана)