Responding to different html requests

I find in my controllers that I often want to respond to two types of
request for html in the same action:

  1. A full page request in which the whole page should be rendered
  2. 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!

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:

  1. A full page request in which the whole page should be rendered
  2. 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:

  1. Use a custom action on a single controller.

http://example.com/products
http://example.com/products/top_selling

  1. 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

  1. 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