views:

35

answers:

2

I have a resource, Inventory, that needs to be "show"ed about 4 different ways depending on the context. What is the best way to tackle this?

I was thinking that I could either pass in a parameter (param[:context]) that would have the "show" action render the right view. Or maybe I should make another controller, though that seems a little much. What are the best practices/general guidelines when you want to stay RESTful but you have a resource that needs to be displayed many different ways?

A: 

I would just use different actions for each type of 'show' that you need for each object, that way you dont have to worry about passing around a context variable and separating which view to render.
Just make sure you have the routes setup right and are linking to the right action for the different context types you setup.

cmpolis
That makes perfect sense, but is there a better, RESTful, alternative?
telecasterrok
A: 

The question is tricky, because there are many alternatives but the answer would depend on what are you trying to do.

Does the context represent something in your model? Then you should use different models, and different controllers.

Does the context represent something other than the REST actions? Add a custom REST action, (http://railscasts.com/episodes/35-custom-rest-actions) with its respective route (seems to me what you're trying to do here).

Are the views equivalent, just with different markup? You can use Cells (http://cells.rubyforge.org/) to abstract your presentation Pattern.

I'd go strongly against creating multiple actions if you don't want to break the RESTful state, but ultimately that can be a solution too.

Chubas
Thanks, Chubas.On one page I'm using ajax to render show.js.erb into a dialog box. This is a basic view of the inventory item.On another page I want to render a slightly different view (via ajax, again into a dialog box) but with different styling and a bit more content (showing more of the fields associated with the inventory item).right now I'm just passing a parameter with the ajax call that specifies the "view_type". depending on the view type, I render either a "mini_view" partial or an "expanded_view" partial in show.js.erb. It works, but is custom actions a better way to go?
telecasterrok
Cells looks cool! I'm looking into it now.
telecasterrok