telecasterrok
telecasterrok

Reputation: 209

REST & Rails: What is the best way to show different views for a given resource and action?

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?

Upvotes: 4

Views: 837

Answers (2)

Chubas
Chubas

Reputation: 18033

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.

Upvotes: 1

cmpolis
cmpolis

Reputation: 3051

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.

Upvotes: 0

Related Questions