想要添加附件的 url,同时响应获取父资源(比如人)的嵌套资源(比如文档)的请求。
# people_controller.rb
def show
render json: @person, include: [{document: {include: :files}}]
end
# returns
# {"id":1,"full_name":"James Bond","document":{"id":12,"files":[{"id":12,"name":"files","record_type":"Document","record_id":689,"blob_id":18,}]}
# MODELS
# person.rb
class Person < ApplicationRecord
has_one :document, class_name: "Document", foreign_key: :document_id
end
# document.rb
class Document < ApplicationRecord
has_many_attached :files
end
问题是,我想在 React 前端设置中显示文件或提供指向文件的链接,它没有像 url_for 这样的辅助方法。正如这里指出的那样。
任何帮助将不胜感激。