The most elegant solution for this that I have seen is to do two things: recognize mobile users based on the user agent in the request and use the custom mime Rails type for response, allowing you to customize HTML templates for mobile users.
Define a custom โmobileโ Rails MIME type in config/initializers/mime_types.rb , which is just HTML:
Mime::Type.register_alias "text/html", :mobile
Add a helper / filter to answer mobile users:
def mobile_user_agent? @mobile_user_agent ||= ( request.env["HTTP_USER_AGENT"] && request.env["HTTP_USER_AGENT"][/(Mobile\/.+Safari)/] ) end
then ..
before_filter :handle_mobile def handle_mobile request.format = :mobile if mobile_user_agent? end
Create your own mobile template:
app/views/users/show.html.erb => app/views/users/show.mobile.erb
Sending mobile or regular in controllers:
respond_to do |format| format.html { }
source share