In Rails, how to make :mobile views to fallback default views when not found?
ruby, ruby-on-rails, ruby-on-rails-3
Solution
assume you got a `mobile_request?` controller instance method to detect mobile requests, then you should be able to set format fallback chains:
# application_controller.rb
before_filter :set_request_format, :set_format_fallbacks
respond_to :html, :mobile # etc
def set_request_format
request.format = :mobile if mobile_request?
end
def set_format_fallbacks
if request.format == :mobile
self.formats = [:mobile, :html]
end
end
This should work but apparently it doesn't completely. https://github.com/rails/rails/issues/3855 If you have a mobile template, the format seems to get locked and it will not find a partial with only html.
Hopefully it will be fixed some way or other. In the meantime you can put this <% controller.set_format_fallbacks %> in each template (ouch) or write your own resolver. http://jkfill.com/2011/03/11/implementing-a-rails-3-view-resolver/
also look at:
Can a mobile mime type fall back to "html" in Rails?
Changing view formats in rails 3.1 (delivering mobile html formats, fallback on normal html)
Problem
This question is a follow up to my previous unanswered question: ActionView::MissingTemplate: Missing template (Trying to render nonexistent :mobile format ) Since doesn't seem to be a Rails approach consensus with this, Is there any way that when accessing from a mobile device to render default `:html` when `:mobile` format is not available? (If a `:mobile` view is present should have priority over those who are not mobile formatted).