【问题标题】:Rails API - how to enable ActiveStorage::DirectUploadsControllerRails API - 如何启用 ActiveStorage::DirectUploadsController
【发布时间】:2018-10-31 11:51:11
【问题描述】:

我正在使用 Rails API (5.2.0),通过发布到 ActiveStorage::DirectUploadsController#create (/rails/active_storage/direct_uploads 路径) 直接从 Ember 应用上传文件时遇到问题。我收到404error。这是否意味着上述控制器在 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

我创建了两个 repos 来重现问题:

  1. Rails API app
  2. Ember App(前端客户端)

【问题讨论】:

  • 我更新了这两个应用程序。现在我可以将图像保存/附加到帖子:post.photo.attached? 返回true,耶!问题是在帖子/索引页面上显示它。当然,我在 Ember environment.js 中定义了 API 主机并将其设置在 application.js 适配器中。尽管如此,Ember 仍在尝试访问lGET http://localhost:4200/rails/active_storage/representations/dXIiOiJibG9iX2lkIn19--50be86904a865aadf1/eyJfcmFpbHMiOnsi/canapex.jpg 404 (Not Found)。在模板中显示帖子列表时会发生这种情况:&lt;img src={{post.photo}} alt="post photo" class="img-thumbnail" height="32" width="32"&gt;.

标签: ember.js rails-api


【解决方案1】:

我将 2 个应用推送到 GitHub:

你必须:

  • 创建DirectUploadsController扩展ActiveStorage::DirectUploadsController
  • 修改对应的模型序列化器(以我的PostSerializer为例)
  • Rails.application.routes.default_url_options[:host] = 'localhost:3000' 添加到development.rb 文件(根据您的环境更新相应的值)

就是这样。 Ember 方面没有更多的调整或特殊更改。

希望这会有所帮助。欢迎任何建议和意见!

【讨论】:

    猜你喜欢
    • 2020-07-31
    • 1970-01-01
    • 2018-10-10
    • 2020-04-04
    • 1970-01-01
    • 2018-11-01
    • 2020-03-27
    • 1970-01-01
    • 2019-05-19
    相关资源
    最近更新 更多