Kendall B. wrote:
def index
@thing = Thing.all
respond_to do |format|
format.first_view
format.second_view
format.third_view
end
end
I have a hunch, however, that separate mime types for different views is
inappropriate, or is this good? Any thoughts?
I have a hunch that your hunch is correct. At least in the sense that
you present here. There is a fundamental concept of the REST
architecture that I believe Rails tends to make somewhat obscure. The
concept I’m referring to is the fundamental definitions of resources and
representations.
It is tempting to think of a “resource” (in Rails) as the combination of
a controller and a model, but that is not really the case.
Take for example this table from Fielding’s Dissertation on REST:
Table 5-1: REST Data Elements
Data Element | Modern Web Examples
resource | the intended conceptual target of a hypertext
reference
resource identifier | URL, URN
representation | HTML document, JPEG image
representation metadata | media type, last-modified time
resource metadata | source link, alternates, vary
control data | if-modified-since, cache-control
Notice the definition of a resource, “The intended conceptual target of
a hypertext reference.” The concept of a resource by definition is a
conceptual target not a specific controller.
In my mind this means that there is not a one-to-one correspondence
between a resource and a controller/model. Therefore, if there are three
separate, and independent views, they each deserve their own “conceptual
target,” and hence their own set of resource identifiers. In Rails this
means they might have their own controllers and their own routes to map
URLs to controller actions.
The responds_to block (and hence mime-types) are intended to provide
access to the representations of a resource. Again these are
representations of the conceptual concept of a resource. As indicated in
the table above this might be in the form of an HTML document, JPEG
image, XML document, JSON, etc.
The types also do not have to be a one-to-one correspondence. One could
very well have multiple representations in HTML. A good use case for
this is to provide alternate layouts for different target devices. One
might define a mime-type for an iPhone representation of a resource
along with a desktop browser version both using HTML.