Note that there are some explanatory texts on larger screens.

plurals
  1. POCan a mobile mime type fall back to "html" in Rails?
    text
    copied!<p>I'm using this code (taken from <a href="http://snipplr.com/view/35063/rails-iphone-detection/" rel="noreferrer">here</a>) in ApplicationController to detect iPhone, iPod Touch and iPad requests: </p> <pre><code>before_filter :detect_mobile_request, :detect_tablet_request protected def detect_mobile_request request.format = :mobile if mobile_request? end def mobile_request? #request.subdomains.first == 'm' request.user_agent =~ /iPhone/ || request.user_agent =~ /iPod/ end def detect_tablet_request request.format = :tablet if tablet_request? end def tablet_request? #request.subdomains.first == 't' request.user_agent =~ /iPad/ end </code></pre> <p>This allows me to have templates like show.html.erb, show.mobile.erb, and show.tablet.erb, which is great, but there's a problem: It seems I must define every template for each mime type. For example, requesting the "show" action from an iPhone without defining show.mobile.erb will throw an error even if show.html.erb is defined. If a mobile or tablet template is missing, I'd like to simply fall back on the html one. It doesn't seem too far fetched since "mobile" is defined as an alias to "text/html" in mime_types.rb.</p> <p>So, a few questions:</p> <ol> <li>Am I doing this wrong? Or, is there a better way to do this?</li> <li>If not, can I get the mobile and tablet mime types to fall back on html if a mobile or tablet file is not present?</li> </ol> <p>If it matters, I'm using Rails 3.0.1. Thanks in advance for any pointers.</p> <p><strong>EDIT:</strong> Something I forgot to mention: I'll eventually be moving to separate sub-domains (as you can see commented out in my example) so the template loading really needs to happen automatically regardless of which <code>before_filter</code> has run.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload