# Rendering different views for a given action

**URL:** <https://www.ruby-forum.com/t/rendering-different-views-for-a-given-action/173975>\
**Category:** Rails\
**Created:** [August 18, 2009, 10:17pm UTC](https://www.ruby-forum.com/t/rendering-different-views-for-a-given-action/173975 "2009-08-18T22:17:27Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![glex](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/f07891/32.png) [@glex](https://www.ruby-forum.com/u/glex)\
**Post date:** [August 18, 2009, 10:17pm UTC](https://www.ruby-forum.com/t/rendering-different-views-for-a-given-action/173975/1 "2009-08-18T22:17:27Z")

</div>

Hi, everyone, this is my first post here!

I have a “HomeController” that has an “index” method.

I have 4 views for this method:

- index.html.erb
- index.mobile\_lofi.erb
- index.mobile\_hifi.erb
- index.iphone.erb

I also have the following mimetypes defined:  
Mime::Type.register\_alias “text/html”, :mobile\_lofi  
Mime::Type.register\_alias “text/html”, :mobile\_hifi  
Mime::Type.register\_alias “text/html”, :iphone

Now, the content (instance variables) these views need is almost the  
same among them.

I have a method “device\_type?” that returns one of the [:html,  
:mobile\_lofi, :mobile\_hifi, :iphone] based on the User-Agent string.

How do I render the view based on the result of the “device\_type?”  
method?  
I am using Rails 2.3.3.

Thank you,  
Gleb

---

<div class="post-metadata">

**Author:** ![glex](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/f07891/32.png) [@glex](https://www.ruby-forum.com/u/glex)\
**Post date:** [August 19, 2009, 7:01am UTC](https://www.ruby-forum.com/t/rendering-different-views-for-a-given-action/173975/2 "2009-08-19T07:01:46Z")

</div>

Gleb M. wrote:

> Hi, everyone, this is my first post here!
> 
> I have a “HomeController” that has an “index” method.
> 
> I have 4 views for this method:
> 
> - index.html.erb
> - index.mobile\_lofi.erb
> - index.mobile\_hifi.erb
> - index.iphone.erb
> 
> I also have the following mimetypes defined:  
> Mime::Type.register\_alias “text/html”, :mobile\_lofi  
> Mime::Type.register\_alias “text/html”, :mobile\_hifi  
> Mime::Type.register\_alias “text/html”, :iphone
> 
> Now, the content (instance variables) these views need is almost the  
> same among them.
> 
> I have a method “device\_type?” that returns one of the [:html,  
> :mobile\_lofi, :mobile\_hifi, :iphone] based on the User-Agent string.
> 
> How do I render the view based on the result of the “device\_type?”  
> method?  
> I am using Rails 2.3.3.
> 
> Thank you,  
> Gleb

Gleb, I am not sure, but i would think along these line

def index  
format = device\_type?  
respond\_to do |format|  
format.html  
format.mobile\_lofi { render blah}  
format.iphone { render :action =\> blah }  
end  
end

Cheers

Craigslist Clone in Rails  
[http://www.classifiedscript.in](http://www.classifiedscript.in)

---

<div class="post-metadata">

**Author:** ![glex](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/f07891/32.png) [@glex](https://www.ruby-forum.com/u/glex)\
**Post date:** [August 19, 2009, 3:53pm UTC](https://www.ruby-forum.com/t/rendering-different-views-for-a-given-action/173975/3 "2009-08-19T15:53:40Z")

</div>

On Wed, Aug 19, 2009 at 1:01 AM, Rails  
List[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> format = device\_type?  
> respond\_to do |format|  
> format.html  
> format.mobile\_lofi { render blah}  
> format.iphone { render :action =\> blah }  
> end  
> end

Close, but I think you need to set request.format, and I’d probably do  
that in a before filter.

class HomeController \< ApplicationController  
before\_filter :set\_device\_mime\_type

```
 def set_device_mime_type
     request.format = device_type?
 end

 def index
     respond_to do |format|
         #what you said
     end
end

```

end

I’d also strongly suggest renaming that device\_type? method to  
something like device\_mime\_type since the question mark indicates that  
it should be a predicate method.

It might also be a good idea to refactor this a bit so that you only  
change the format of the request if it is coming from one of the  
special devices, and leave it to what the client sent otherwise.

–  
Rick DeNatale

Blog: [http://talklikeaduck.denhaven2.com/](http://talklikeaduck.denhaven2.com/)  
Twitter: [http://twitter.com/RickDeNatale](http://twitter.com/RickDeNatale)  
WWR: [http://www.workingwithrails.com/person/9021-rick-denatale](http://www.workingwithrails.com/person/9021-rick-denatale)  
LinkedIn: [http://www.linkedin.com/in/rickdenatale](http://www.linkedin.com/in/rickdenatale)

---

<div class="post-metadata">

**Author:** ![glex](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/f07891/32.png) [@glex](https://www.ruby-forum.com/u/glex)\
**Post date:** [August 19, 2009, 6:13pm UTC](https://www.ruby-forum.com/t/rendering-different-views-for-a-given-action/173975/4 "2009-08-19T18:13:54Z")

</div>

Thanks a lot, Rick!
