title: Changing view formats in rails 3.1 (delivering mobile html formats, fallback on normal html)
body: <p>I'm creating a mobile site next to our normal html site. Using rails 3.1. Mobile site is accessed in subdomain m.site.com.</p>
  
  <p>I have defined mobile format (Mime::Type.register_alias "text/html", :mobile).</p>
  
  <p>In ApplicationController I have "before_filter :mobile_site_before_filter" that recognizes mobile site and sets format according to it.</p>
  
  <pre><code>def mobile_site_before_filter
    if request.subdomains.first == 'm'
      original_format = request.format
      request.format = :mobile
      @mobile_site = true
      request.formats.push(original_format)
    end
  end
  </code></pre>
  
  <p>In layouts I have 'main.html.erb' and 'main.mobile.erb'. So with mobile sites mobile layout is used.</p>
  
  <p>Now it kind of works ok.</p>
  
  <p>In UserController I have index action which chooses index.html.erb or index.mobile.erb automatically. No extra coding in top of mobile-view needed. Success.</p>
  
  <p>But I have lots of other views where the same template could be used to serve inside mobile layouts but with minor changes.</p>
  
  <p>E.g. in MessagesController the same view would be almost fine for mobile</p>
  
  <pre><code>In index.html.erb
  Normal user info, common for mobile and html
  &lt;%= render(:partial =&gt; 'messages') %&gt;
  </code></pre>
  
  <p>Rendering messages.html.erb, no need for messages.mobile.erb. mobile view can be done with css </p>
  
  <pre><code>&lt;%# By default self.formats = [:html] because this is .html.erb partial, even in mobile site %&gt;
  &lt;%= self.formats = [:mobile, :html] if @mobile_site # How to get rid of this? %&gt;
  &lt;%= render(:partial =&gt; 'vote_form') %&gt;
  &lt;!-- rest of page ... --&gt;
  </code></pre>
  
  <p>Now i want to render vote_form.[mobile|html].erb depending on the site...</p>
  
  <p>Now the index.html.erb partial would be fine to use with mobile if I could just choose between vote_form.html.erb or vote_form.mobile.erb. I can <a href="https://stackoverflow.com/a/6181539/783851">choose to use mobile partial</a> with using
  "self.formats = [:mobile, :html] if @mobile_site" in the beginning of index.html.erb . But it feels stupid to write this in beginning of all templates. </p>
  
  <p><strong>So the question is:</strong></p>
  
  <ul>
  <li><strong>Where does the self.formats come in views (what sets it originally) and how can I set that it's always [:mobile, :html] inside the mobile site? Why isn't it the same as i set in controller before_filter? Can I set it somehow in the controller already?</strong></li>
  </ul>
  
  <p>(minor bonus questions)</p>
  
  <ul>
  <li><p>Is there something wrong in this approach? Using mostly the same html-views and in some specific cases using mobile.erb views instead. Why doesn't this work by default in rails?</p></li>
  <li><p>Is there other ways to choose to render :mobile views if found with fallback to normal html view? Even across the partials so that html.erb-view tries to use _partial.mobile.erb partials.</p></li>
  </ul>