2013-02-18 138 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