You are here

Choosing Layouts at Runtime in Ruby on Rails - iPhone Safari x Firefox or IE on a PC

I wanted to set a different layout in my application depending on a decision of a special situation. For example, if the client system is a browser on an iPhone I'd like to show a different layout than a Firefox running in a PC.

I found inspiration on the official RoR documentation, specifically on the page about the rendering mechanism. Here is the code I wrote to display a specific layout if the client browser is a Safari running in a iPhone or iPod touch.

All you have to do is add some methods to your ApplicationController and have another layout file in your /views/layouts folder. Check the commented code below:

class ApplicationController 

Comments

I found this article from IBM which might be a more robust/complete (although more complex) solution to the one I mentioned above.

Developing iPhone applications using Ruby on Rails and Eclipse, Part 1: Serving content for iPhones: http://www.ibm.com/developerworks/opensource/library/os-eclipse-iphoneru...

Thanks a lot. This was short and to the point, just what I needed!