Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><strong>I have added a new answer for version 3.2.X.</strong> This answer is valid for &lt;~ 3.0.1.</p> <p>I came to this question while looking to be able to have multiple fallbacks on the view. For example if my product can be white-labeled and in turn if my white-label partner is able to sell sponsorship, then I need a cascade of views on every page like this:</p> <ul> <li>Sponsor View: .sponsor_html</li> <li>Partner View: .partner_html</li> <li>Default View: .html</li> </ul> <p>The answer by Joe, of just removing .html works (really well) if you only have one level above the default, but in actual application I needed 5 levels in some cases.</p> <p>There did not seem to be anyway to implement this short of some monkey patching in the same vein as Jeremy. </p> <p>The Rails core makes some fairly wide ranging assumptions that you only want one format and that it maps to a single extension (with the default of NO extension).</p> <p>I needed a single solution that would work for all view elements -- layouts, templates, and partials.</p> <p>Attempting to make this more along the lines of convention I came up with the following.</p> <pre><code># app/config/initializers/resolver.rb module ActionView class Base cattr_accessor :extension_fallbacks @@extension_fallbacks = nil end class PathResolver &lt; Resolver private def find_templates_with_fallbacks(name, prefix, partial, details) fallbacks = Rails.application.config.action_view.extension_fallbacks format = details[:formats].first unless fallbacks &amp;&amp; fallbacks[format] return find_templates_without_fallbacks(name, prefix, partial, details) end deets = details.dup deets[:formats] = fallbacks[format] path = build_path(name, prefix, partial, deets) query(path, EXTENSION_ORDER.map {|ext| deets[ext] }, details[:formats]) end alias_method_chain :find_templates, :fallbacks end end # config/application.rb config.after_initialize do config.action_view.extension_fallbacks = { html: [:sponsor_html, :partner_html, :html], mobile: [:sponsor_mobile, :partner_mobile, :sponsor_html, :partner_html, :html] } # config/initializers/mime_types.rb register_alias 'text/html', :mobile # app/controllers/examples_controller.rb class ExamplesController respond_to :html, :mobile def index @examples = Examples.all respond_with(@examples) end end </code></pre> <p>Note: I did see the comments around alias_method_chain, and initially did make a call to super at the appropriate spot. This actually called ActionView::Resolver#find_templates (which raises a NotImplemented exception) rather than the ActionView::PathResolver#find_templates in some cases. I wasn't patient enough to track down why. I suspect its because of being a private method.</p> <p>Plus, Rails, at this time, does not report alias_method_chain as deprecated. Just that post does.</p> <p>I do not <em>like</em> this answer as it involves some very brittle implementation around that find_templates call. In particular the assumption that you only have ONE format, but this is an assumption made all over the place in the template request.</p> <p>After 4 days of trying to solve this and combing through the whole of the template request stack its the best I can come up with.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

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