我正在使用Rails API(5.2.0
),并通过发布到ActiveStorage :: DirectUploadsController#create(/rails/active_storage/direct_uploads
路径)直接从Ember应用程序直接上传文件时遇到问题。我遇到404
错误。这是否意味着以上控制器在Rails API模式应用程序中不可用?如果是这样,如何包括相应的模块?还是我必须创建一个继承自ActiveStorage::DirectUploadsController
的自定义控制器?
我创建了DirectUploadsController
并扩展了ActiveStorage::DirectUploadsController
,如下所示:
class DirectUploadsController < ActiveStorage::DirectUploadsController
protect_from_forgery with: :exception
skip_before_action :verify_authenticity_token
end
并定义了Post模型:
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
我创建了两个存储库来重现该问题:
答案 0 :(得分:0)
我将2个应用程序推送到GitHub:
您将必须:
DirectUploadsController
的{{1}} ActiveStorage::DirectUploadsController
添加到development.rb文件中(根据您的环境更新相应的值)仅此而已。 Ember端不再有任何调整或特殊更改。
希望这会有所帮助。欢迎任何建议和评论!