# One Controller, many viewing platforms

**URL:** <https://www.ruby-forum.com/t/one-controller-many-viewing-platforms/110407>\
**Category:** Rails\
**Created:** [July 26, 2007, 2:13am UTC](https://www.ruby-forum.com/t/one-controller-many-viewing-platforms/110407 "2007-07-26T02:13:35Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![cidol](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/54ee81/32.png) [@cidol](https://www.ruby-forum.com/u/cidol)\
**Post date:** [July 26, 2007, 2:13am UTC](https://www.ruby-forum.com/t/one-controller-many-viewing-platforms/110407/1 "2007-07-26T02:13:35Z")

</div>

At our office we’re building an app with some CMS-like qualities.

We have the requirement of serving to multiple platforms. We can strip  
enough information out of the Headers to determine which platform a  
request comes from.

Keeping with best practices we don’t want to add any controllers for  
this View problem. This should keep things nicely separated and  
provide the benefit of all devices accessing the same URLs.

The structure of our view folders then looks like this:

\views  
\some\_controller  
\platform\_one  
\platform\_two  
\platform\_three

The problem now is to get get rails to go from this:

GET …/some\_controller/

> > Rails renders …/views/some\_controller/index.rhtml

to this:

header indicating platform\_one  
GET …/some\_controller/

> > Rails renders …/views/some\_controller/index.rhtml

A first attempt at such a solution rendered the following:

class ApplicationController \< ActionController::Base  
private

# This method is stolen from ActionController::Base

# The only additon is “+ platform”

def controller\_path  
self.class.name.gsub(/Controller$/,’’).underscore + platform  
end

def platform  
## This method returns a string derived from the request  
## This string will be added to the controller\_path used to find  
templates  
end

## default\_template\_name and template\_path\_includes\_controller? have

been

## copied from ActionController::Base

## There is one small change.

## references to “self.class.controller\_path” have been swapped for

“controller\_path”  
def default\_template\_name(action\_name = self.action\_name)  
if action\_name  
action\_name = action\_name.to\_s  
if action\_name.include?(’/’) &&  
template\_path\_includes\_controller?(action\_name)  
action\_name = strip\_out\_controller(action\_name)  
end  
end  
“#{controller\_path}/#{action\_name}”  
end

def template\_path\_includes\_controller?(path)  
controller\_path.split(’/’)[-1] == path.split(’/’)[0]  
end  
end

By overriding Rails where it builds a templates file path string we  
can have platform specific views for our controllers without changing  
individual controller code.

This solution is by no means tested, final or complete, just a peek  
into what can be done.

A concern was raised in the office:

The performance hit taken checking the platform with every request.

I think the hit is negligible and will scale linearly. Still, should  
that become an issue the platform can be worked into the session and  
care can be taken that the platform-aware controller path will not be  
generated more than once per request

I brought this to the mailing list in hopes of getting some feedback.

We are early in looking into filling this requirement and would be  
overjoyed if a solution already exists or if there is another path to  
take toward it.

Does anybody have any thoughts, negative or positive about this?

---

<div class="post-metadata">

**Author:** ![cidol](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/54ee81/32.png) [@cidol](https://www.ruby-forum.com/u/cidol)\
**Post date:** [July 26, 2007, 6:38am UTC](https://www.ruby-forum.com/t/one-controller-many-viewing-platforms/110407/2 "2007-07-26T06:38:51Z")

</div>

On Jul 25, 8:12 pm, Collin M. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> A concern was raised in the office:
> 
> The performance hit taken checking the platform with every request.
> 
> I think the hit is negligible and will scale linearly. Still, should  
> that become an issue the platform can be worked into the session and  
> care can be taken that the platform-aware controller path will not be  
> generated more than once per request

Unless your platform method is time-consuming, storing it in the  
session won’t gain you anything.

A good measurement for “time-consuming” would be to compare it to a  
database query. More than likely it is faster than a database query.  
Therefore you are better off reducing database queries than worrying  
about to storing and reading the platform from the session.

~Rusty
