我怎么能用我的帖子(每个帖子都有一个附件)和他们的图片网址渲染一个json?
def index
@posts = Post.all
render json: { posts: @posts }, include: :image # Image is the attachment
end
我有这个,但我知道这行不通,因为我需要图片 URL
最佳答案
json.jbuilder View 文件可以在这里提供帮助
app/view/posts/index.json.jbuilder
json.array! @posts, partial: 'post', as: :post
app/view/posts/_post.json.jbuilder
json.id post.id
json.image_url url_for(post.image) if post.image.attached?
//all other fields you need
关于ruby-on-rails - 渲染 json 中的 Rails Active Storage 图像链接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52839294/