*   \   &   ©   |   /   * *   \   &   ©   |   /   * *   \   &   ©   |   /   * *   \   &   ©   |   /   * *   \   &   ©   |   /   *

posts tagged 'javascript'

Always render Rails views without the full layout when using AJAX & degradable javascript

Rails-fu

Been neck-deep in Ruby on Rails the last few months, here’s my first nugget o’ code wisdom:

With unobtrusive javascript all of my Rails views may or may not be getting called via AJAX. Adding “:layout => :false if request.xhr?” all the time was bad code smell.

Here’s a 4 liner to extend the ‘render’ method and check if the view, partial, EJS etc. are being called via an XmlHttpRequest, in which case we want to ignore the layout unless explicitly asked to. Throw it in your ApplicationController and degrade away!


def render(*args)
  	args.first[:layout] = false if request.xhr? and args.first[:layout].nil?
	super
end

Might package this little guy into a plugin I find it so handy