2

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

用户/show.rabl

object @user
    attributes :id, :name

    child :posts do
        extends "posts/post"
    end

帖子/_post.rabl

attributes :id, image: post.image.url

我想显示帖子的图像,但我有一个错误:未定义的局部变量或方法“帖子”

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

蜻蜓宝石加载的图像。

如何以 json 格式获取此图像 url?

4

1 回答 1

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
于 2013-02-19T10:48:00.890 回答