开发者

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

开发者 https://www.devze.com 2023-01-11 04:56 出处:网络
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 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 h开发者_Python百科ave a resource that needs to be displayed many different ways?


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消