2

RABL gem を使用しています。ユーザー モデルには多くの投稿があります。

users/show.rabl

object @user
    attributes :id, :name

    child :posts do
        extends "posts/post"
    end

posts/_post.rabl

attributes :id, image: post.image.url

投稿の画像を表示したいのですが、エラーがあります: undefined local variable or method "post" .

しかし、posts/_post.html.erb では、この <%= post.image.url =%> を使用できます

トンボの宝石によって読み込まれた画像。

この画像の URL を json 形式で取得するにはどうすればよいですか?

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 に答える