Created
November 26, 2013 15:32
-
-
Save th3hunt/7660458 to your computer and use it in GitHub Desktop.
From scottwb, A Better Way to Add Mobile Pages to a Rails Site
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# app/controllers/application_controller.rb | |
class ApplicationController < ActionController::Base | |
def check_for_mobile | |
session[:mobile_override] = params[:mobile] if params[:mobile] | |
prepare_for_mobile if mobile_device? | |
end | |
def prepare_for_mobile | |
prepend_view_path Rails.root + 'app' + 'views_mobile' | |
end | |
def mobile_device? | |
if session[:mobile_override] | |
session[:mobile_override] == "1" | |
else | |
# Season this regexp to taste. I prefer to treat iPad as non-mobile. | |
(request.user_agent =~ /Mobile|webOS) && (request.user_agent !~ /iPad/) | |
end | |
end | |
helper_method :mobile_device? | |
end | |
# app/controllers/posts_controller.rb | |
class PostsController < ApplicationController | |
# Render mobile or desktop depending on User-Agent for these actions. | |
before_filter :check_for_mobile, :only => [:new, :edit] | |
# Always render mobile versions for these, regardless of User-Agent. | |
before_filter :prepare_for_mobile, :only => :show | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment