0

I am working on a Rails app that declares a :mobile format for iPhone and Android and serves up show.mobile.haml for mobile and show.html.haml for web (for example).

The mobile request format obviously uses application.mobile.haml and web uses application.html.haml - but both layouts are the same, only the views differ.

My question is - how do I use a single application layout for both the mobile and html request formats? Have dug through the Rails API documentation and can't seem to find an obvious solution.

buddhamagnet
  • 230
  • 2
  • 11

1 Answers1

0

You don't need to do anything. This happens automaticly when you add an extension to your urls. If you go to /controller/action.mobile the action.mobile.haml view will be rendered. If you go to /controller/action.html the action.html.haml view will be rendered.

This behaviour is managed by the format parameter (you can see this in your routes file). So /controller/action.mobile is the same as /controller/action?format=mobile.

Ofcourse, sometimes you'll want your actions to behave differently depending of the current format. This is supported be using the respond_to and respond_with method. More info about these can be found here.

Stefaan Colman
  • 3,715
  • 2
  • 22
  • 11
  • I think Dave is trying to avoid the need to have both application.mobile and application.html layout files? Can you cheat have one include the other? – John Beynon Jan 14 '11 at 13:22
  • For that look here: http://stackoverflow.com/questions/339130/how-do-i-render-a-partial-of-a-different-format-in-rails – Stefaan Colman Jan 14 '11 at 13:33
  • Many thanks Stefaan. Yep, John is correct. I understand your answer - I have a set of views that do differ so, for example, I have show.html.haml and show.mobile haml. But the application layout is dupliacted in application.mobile.haml and application.html.haml and I want to serve up the same layout for both :mobile and :html request formats. I don't consider this cheating, just want to DRY up the code. And I don't want to server up the same application layout for ALL formats, just :mobile and :web. – buddhamagnet Jan 16 '11 at 11:07