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

I have an Inventory resource that needs to be "shown" in 4 different ways depending on the context. What is the best way to handle this?

I thought that I can either pass in the parameter (param [: context]) so that the "show" action displays the correct view. Or maybe I should do another controller, although that seems a bit. What are the best practices / general guidelines when you want to stay RESTful, but do you have a resource that needs to be displayed in different ways?

+4
source share
2 answers

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

Is context present in your model? Then you should use different models and different controllers.

Is context something other than REST actions? Add a custom REST action ( http://railscasts.com/episodes/35-custom-rest-actions ) with the appropriate route (it seems to me what you are trying to do here).

Are representations equivalent, only with different markup? You can use cells ( http://cells.rubyforge.org/ ) to distract your presentation template.

I would strongly oppose creating multiple actions if you do not want to break the RESTful state, but in the end it can also be a solution.

+1
source

I would just use different actions for each type of β€œshow” that you need for each object, so you don’t have to worry about passing around the context variable and separating which view to render.
Just make sure that you have configured the routes correctly and are attached to the correct action for the different types of context that you are setting.

0
source

All Articles