Skip to content

Instantly share code, notes, and snippets.

@dariodaich
Last active November 30, 2016 08:14
Show Gist options
  • Save dariodaich/a93068c5cac286044a500b35005e9841 to your computer and use it in GitHub Desktop.
Save dariodaich/a93068c5cac286044a500b35005e9841 to your computer and use it in GitHub Desktop.
Render different templates using single action.

Action Pack Variants

Basecamp had a problem. How did DHH solve it?

  • the problem : how to serve different view templates to their hybrid mobile apps and desktop clients at the same time?
  • the solution: introduce a new feature to Rails. One that will enable single action to serve different templates.

How is it done?

  • conceptually: "variant" is a symbol whose name constitutes a part of the variant naming scheme
  • naming scheme is: action_name.html+variant_name.erb -> new.html+mobile.erb
  • technically: a symbol, or an array of symbols, that get assigned with a call to request.variant=
  • use request.user_agent to test the client type
  • if no "variant template" is found, the default action template gets rendered
  • it works for partials just the same
  • it exposes browser object to the ActionController
  • transparent API:
  • browser.chrome?
  • browser.bot.search_engine?
  • browser.device.mobile?
  • browser.platform.linux?

Resources

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment