Take a look at Action Pack Variants
feature built into Rails 4.1. This gives you the familiar respond_to block but for different clients:
respond_to do |format|
format.html do |html|
html.tablet # renders app/views/projects/show.html+tablet.erb
html.phone { extra_setup; render ... }
end
endAnd your views get named something like:
app/views/projects/show.html.erb
app/views/projects/show.html+tablet.erb
app/views/projects/show.html+phone.erb
If that doesn't help, then read on...
Let's say you're going to start building a mobile version of your site. You use something like
mobile fu which gives you a new format, :mobile, which
is automatically set if a request comes in from a mobile device.
All you need to do to start serving your mobile pages is to create a .mobile version of your views:
index.html.erb -> index.mobile.erb
Great! The only downside is that now you need .mobile versions of every view in your site right
away. Even worse, you need .mobile version of every partial in your site. Sometimes you might
just want a new application layout for mobile and let everything else stay just the way it is.
Enter Format Fallback.
Format Fallback simply tries to get a .html version of a view if the initial format lookup fails.
So if Rails tries to get index.mobile.erb it will normally throw a MissingTemplate error. Now it will
switch formats to .html and try to get that instead. Then, if .html isn't found, you'll get a
MissingTemplate error (which will, by the way, list the original format that couldn't be found,
not .html).
Enjoy!
Format Fallback doesn't currently let you provide any options to say, for example, "only fall back
to :html if :mobile isn't found." It will attempt to fall back for any template that isn't found.
Rails is generally trying to help you by telling you that a given template is missing so that you
know you have to go in and add one. Which means if you're working on some new :csv versions of your
pages but forget to add one, and there is a requisite :html version, Rails will now serve the :html
version automatically. Someone would probably find this in testing, but it's something to be aware of.
Right now this gem is locked to Rails 3.2