JSON中的图片网址(RABL,Dragonfly,Ruby on Rails) [英] Image url in JSON (RABL, Dragonfly, Ruby on Rails)

查看:79
本文介绍了JSON中的图片网址(RABL,Dragonfly,Ruby on Rails)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用RABL gem,我的用户模型中有很多帖子

I'm using RABL gem, my user model has many posts

users/show.rabl

object @user
    attributes :id, :name

    child :posts do
        extends "posts/post"
    end

posts/_post.rabl

attributes :id, image: post.image.url

我想显示帖子的图像,但出现错误:未定义的局部变量或方法"post" .

I want to show post's image, but i have an error: undefined local variable or method "post".

但是在posts/_post.html.erb中,我可以使用此<%= post.image.url =%>

But in posts/_post.html.erb i can use this <%= post.image.url =%>

蜻蜓宝石装载的图像.

如何获取json格式的图片网址?

How can i get this image url in json format?

推荐答案

如果这是子属性,则可以使用胶将其附加回根节点.

If this is a child-attribute you can append it back to the root node by using glue.

赞:

@post
attributes :id, :image_url

glue @image do
  attributes :url => :image_url
end

或者也许是这样:

@post
attributes :id

node :image_url do |post|
  post.image.url
end

这篇关于JSON中的图片网址(RABL,Dragonfly,Ruby on Rails)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆