Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p><a href="https://stackoverflow.com/questions/8863257/changing-view-formats-in-rails-3-1-delivering-mobile-html-formats-fallback-on/9493617#9493617">Possible Duplicate of</a> <a href="https://stackoverflow.com/questions/8863257/changing-view-formats-in-rails-3-1-delivering-mobile-html-formats-fallback-on/9493617#9493617">Changing view formats in rails 3.1 (delivering mobile html formats, fallback on normal html)</a></p> <p>However, I struggled with this exact same problem and came up with a fairly elegant solution that met my needs perfectly. Here is my answer from the other post. </p> <p>I think I've found the best way to do this. I was attempting the same thing that you were, but then I remembered that in rails 3.1 introduced <a href="http://railscasts.com/episodes/269-template-inheritance" rel="nofollow noreferrer">template inheritance</a>, which is exactly what we need for something like this to work. I really can't take much credit for this implementation as its all laid out there in that railscasts link by Ryan Bates.</p> <p>So this is basically how it goes. </p> <p>Create a subdirectory in <code>app/views</code>. I labeled mine <code>mobile</code>.</p> <p>Nest all view templates you want to override in the same structure format that they would be in the views directory. <code>views/posts/index.html.erb -&gt; views/mobile/posts/index.html.erb</code></p> <p>Create a <code>before_filter</code> in your <code>Application_Controller</code> and do something to this effect.</p> <pre><code> before_filter :prep_mobile def is_mobile? request.user_agent =~ /Mobile|webOS|iPhone/ end def prep_mobile prepend_view_path "app/views/mobile" if is_mobile? end </code></pre> <p>Once thats done, your files will default to the mobile views if they are on a mobile device and fallback to the regular templates if a mobile one is not present. </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