creating one application layout for multiple formats
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.
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.
精彩评论