ストーリーモデルとポストモデルがあります。各ストーリーには多くの投稿があります。
私のstory#indexアクションでは、rablを使用してすべての@storiesを返したいと思います。また、@ storiesの各ストーリーには、最近作成された投稿の属性を含む子ノードも含める必要があります。
これが私が探しているものを達成するために私が到達した最も近いものです:
#show.json.rabl
object @story
attributes :id, :title, :username
child @story.posts.first => :latest_post do
attributes :id, :story_id, :contents, :username
end
これは、stories#showアクションで正常に機能します。しかし、stories#indexアクションで拡張しようとすると
#index.json.rabl
collection @stories
extends "stories/show"
次のエラーが発生します。
ActionView::Template::Error (undefined method `posts' for nil:NilClass):
1: collection @stories
2:
3: extends "stories/show"
app/views/stories/show.json.rabl:8:in `render'
show.json.rablビューで使用される@storyインスタンス変数を設定していないため、このエラーが発生すると思います。しかし、これを回避して、show.json.rablに正しいストーリーオブジェクトを渡す方法がわかりません。
どんな助けでも大歓迎です!