DRYing up Rails Views with Nested Resources

Posted by viatropos on Stack Overflow See other posts from Stack Overflow or by viatropos
Published on 2010-03-22T21:12:17Z Indexed on 2010/03/22 23:01 UTC
Read the original article Hit count: 245

What is your solution to the problem if you have a model that is both not-nested and nested, such as products:

a "Product" can belong_to say an "Event", and a Product can also just be independent.

This means I can have routes like this:

map.resources :products # /products
map.resources :events do |event|
  event.resources :products # /events/1/products
end

How do you handle that in your views properly?

Note: this is for an admin panel. I want to be able to have a "Create Event" page, with a side panel for creating tickets (Product), forms, and checking who's rsvp'd. So you'd click on the "Event Tickets" side panel button, and it'd take you to /events/my-new-event/tickets. But there's also a root "Products" tab for the admin panel, which could list tickets and other random products. The 'tickets' and 'products' views look 90% the same, but the tickets will have some info about the event it belongs to.

It seems like I'd have to have views like this:

  • products/index.haml
  • products/show.haml
  • events/products/index.haml
  • events/products/show.haml

But that doesn't seem DRY. Or I could have conditionals checking to see if the product had an Event (@product.event.nil?), but then the views would be hard to understand.

How do you deal with these situations?

Thanks so much.

© Stack Overflow or respective owner

Related posts about ruby-on-rails

Related posts about views