I find in my controllers that I often want to respond to two types of
request for html in the same action:
A full page request in which the whole page should be rendered
A request in which a partial should be rendered without the whole
layout
I have handled this by passing a parameter in the url to indicate which
one to return but that feels ugly and I wonder if there are better ways?
Thanks!
mikari
December 8, 2011, 9:19pm
2
Stephen M. wrote in post #1035762:
I find in my controllers that I often want to respond to two types of
request for html in the same action:
A full page request in which the whole page should be rendered
A request in which a partial should be rendered without the whole
layout
I have handled this by passing a parameter in the url to indicate which
one to return but that feels ugly and I wonder if there are better ways?
There are a few possibilities.
Say you wanted a standard products page and a partial to use on the
welcome page for displaying the top selling products:
Use a custom action on a single controller.
http://example.com/products
http://example.com/products/top_selling
Use separate controllers. There nothing to say that models and
controllers must exists in a one-to-one relationship. You can have as
many controllers as you want for a single model.
http://example.com/products
http://example.com/top_selling_products
products_controller.rb
top_selling_products_controller.rb
Use separate formats. This is a good option in cases where you want
multiple HTML representations for a single resource. Say for example,
one for desktop browsers and one for mobile browsers.
http://example.com/orders
http://example.com/orders.iphone
http://example.com/orders.mobile