I’m currently developing a gem that handles mobile devices.
The controller looks like this:
class PageController < ApplicationController
has_mobile_views
And the code of the gem looks like this:
module MobileViewsController
module ClassMethods
def has_mobile_views(args={})
class_eval do
if request.subdomain == 'm'
request.format = :mobile_html
layout Proc.new { |controller| controller.request.subdomain == 'm' ? 'mobile_application' : 'application'}
end
end
end
end
The format works just fine, it renders xxx.mobile_html.haml files nicely but what doesn’t work is loading the layout. It doesn’t load any layout whatsoever.
But on the other hand when I change
controller.request.subdomain == 'm' ? 'mobile_application' : 'application'
to
controller.request.subdomain == 'm' ? 'xmobile_application' : 'application'
It throws an error that xmobile_application cannot be found so it must at least look for it.
This also doesn’t work:
class PageController < ApplicationController
has_mobile_views
layout 'mobile_application'
However when I change the PageController to
class PageController < ApplicationController
layout 'mobile_application'
The layout is being loaded and rendered correctly.
Anyone an idea what could be wrong here or what to change in the gem to not screw up the layout?
Assuming you have
mobile_htmlmime type registered inconfig/initializers/mime-types.rbapp/views/layouts/mobile_application.mobile_html.erbMobileViewsmodule included in yourApplicationControllerthat does the job: