2013-02-18 105 views
2

我使用Rabl的宝石,我的用户模型有很多帖子图片网址

用户/ show.rabl

object @user 
    attributes :id, :name 

    child :posts do 
     extends "posts/post" 
    end 

职位/ _post.rabl

attributes :id, image: post.image.url 

我想展后的形象,但我有一个错误:未定义的局部变量或方法“post”

但是在文章/ _post.html.erb我可以使用通过蜻蜓宝石加载这个<%= post.image.url =%>

图像

我怎么能得到JSON格式这一形象网址是什么?

回答

2

如果这是一个孩子的属性可以通过使用胶水追加回根节点。

像这样:

@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