user2759575
user2759575

Reputation: 553

Going backwards in nested resources

I have 3 models: posts, comments and questions. I am trying to show the title of a post that a question belongs to on the question index page. In a sense go backwards through the resources. Here is what the routes look like:

resources :posts do
resources :comments do
 end
end

resources :comments do
 resources :questions do
 end
end

Right now this is how i am trying to do it in my questions index page but am getting an 'undefined method title error':

 <% @post.title %>

Thanks!

Upvotes: 0

Views: 37

Answers (1)

Leger
Leger

Reputation: 1184

Seems, that @post is nil on question index page. Routes just help with routing, nestings should be wired within models via has_many and belongs_to.

To "go back" try @question.post.title

BTW, do you have title field at Post model?

Upvotes: 1

Related Questions