# Hypothetical Question about Views

**URL:** <https://www.ruby-forum.com/t/hypothetical-question-about-views/175505>\
**Category:** Rails\
**Created:** [September 14, 2009, 10:44pm UTC](https://www.ruby-forum.com/t/hypothetical-question-about-views/175505 "2009-09-14T22:44:58Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![mercurykjb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/a9adbd/32.png) [@mercurykjb](https://www.ruby-forum.com/u/mercurykjb)\
**Post date:** [September 14, 2009, 10:44pm UTC](https://www.ruby-forum.com/t/hypothetical-question-about-views/175505/1 "2009-09-14T22:44:58Z")

</div>

This is purely for curiosity’s sake:

If I have a simple controller method:

def index  
@thing = Thing.all

respond\_to do |format|  
format.js  
end  
end

And I’d like to call the ‘index’ controller method from three different  
locations, all with AJAX, to return three entirely different views.

Currently I use this:

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?

---

<div class="post-metadata">

**Author:** ![mercurykjb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/a9adbd/32.png) [@mercurykjb](https://www.ruby-forum.com/u/mercurykjb)\
**Post date:** [September 14, 2009, 11:12pm UTC](https://www.ruby-forum.com/t/hypothetical-question-about-views/175505/2 "2009-09-14T23:12:38Z")

</div>

On Sep 14, 9:44 pm, Kendall B. \<rails-mailing-l…@andreas-  
[s.net](http://s.net)\> wrote:

> I have a hunch, however, that separate mime types for different views is  
> inappropriate, or is this good? Any thoughts?

Sounds like a needless complication to me - I don’t see what this buys  
you.

Fred

---

<div class="post-metadata">

**Author:** ![mercurykjb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/a9adbd/32.png) [@mercurykjb](https://www.ruby-forum.com/u/mercurykjb)\
**Post date:** [September 14, 2009, 11:54pm UTC](https://www.ruby-forum.com/t/hypothetical-question-about-views/175505/3 "2009-09-14T23:54:19Z")

</div>

Frederick C. wrote:

> On Sep 14, 9:44ï¿½pm, Kendall B. \<rails-mailing-l…@andreas-  
> [s.net](http://s.net)\> wrote:
> 
> > I have a hunch, however, that separate mime types for different views is  
> > inappropriate, or is this good? Any thoughts?
> 
> Sounds like a needless complication to me - I don’t see what this buys  
> you.
> 
> Fred

How do you deal with wanting three different views for the same data? Do  
you use three separate controller actions?

---

<div class="post-metadata">

**Author:** ![mercurykjb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/a9adbd/32.png) [@mercurykjb](https://www.ruby-forum.com/u/mercurykjb)\
**Post date:** [September 15, 2009, 2:19am UTC](https://www.ruby-forum.com/t/hypothetical-question-about-views/175505/4 "2009-09-15T02:19:13Z")

</div>

If it is 3 views of the same data, then the controller could just look  
at a params[:which\_format] value to determine where to route the view  
for rendering.

Creating mime types for different views _all destined for the same  
recipient format_ is, in my opinion, an inappropriate use of the mime  
types.

And just a niggle… Thing.all implies you are retrieving @things,  
plural.

---

<div class="post-metadata">

**Author:** ![mercurykjb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/a9adbd/32.png) [@mercurykjb](https://www.ruby-forum.com/u/mercurykjb)\
**Post date:** [September 15, 2009, 4:13pm UTC](https://www.ruby-forum.com/t/hypothetical-question-about-views/175505/5 "2009-09-15T16:13:45Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![mercurykjb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/a9adbd/32.png) [@mercurykjb](https://www.ruby-forum.com/u/mercurykjb)\
**Post date:** [September 15, 2009, 5:01pm UTC](https://www.ruby-forum.com/t/hypothetical-question-about-views/175505/6 "2009-09-15T17:01:32Z")

</div>

On Sep 14, 10:54 pm, Kendall B. \<rails-mailing-l…@andreas-  
[s.net](http://s.net)\> wrote:

> Frederick C. wrote:

> How do you deal with wanting three different views for the same data? Do  
> you use three separate controller actions?

I would. Sounds like it would be clearer than fiddling with  
respond\_to. Obviously you’d want to share the data generation stuff  
between the various actions.

Fred
