iOS/mobile detection with devise
So I'm using User Agent detection to serve different views within my app by serving example.mobile.haml
rather than example.html.haml
. It's all working fine apart from some funny things happening with devise views.
When I hit the root as a desktop UA (Safari, Mac OS) it correctly redirects to /login with a flash, when I do this as a mobile UA it doesn't redirect and displays only the flash and renders no additional HTML.
Similarly, hitting /logout as a desktop UA correctly logs out and redirects to /login as it should. But doing this as a mobile UA hits /logout, destroys your session but doesn't redirect.
I've followed all the instructions on the devise wiki but this is al开发者_如何学JAVAl that won't work. The code that does all of the detection is below:
#application_controller.rb
def format_for_mobile
request.format = :mobile if mobile_user_agent?
end
def mobile_user_agent?
request.env["HTTP_USER_AGENT"]
request.env["HTTP_USER_AGENT"][MOBILE_USER_AGENTS]
end
# config/initializers/devise.rb
config.navigational_formats = [:"*/*", "*/*", :html, :to_mobile]
# config/initializers/mobile_responder.rb
ActionController::Responder.class_eval do
alias :to_mobile :to_html
end
Managed to figure this out myself. The before_filter :format_for_mobile
was before authenticate user, that fixed the /login issue.
For /logout, I had to do the following:
# controllers/sessions_controller.rb
class SessionsController < Devise::SessionsController
before_filter :format_for_mobile, :except => :destroy
end
精彩评论