rubylayoutramaze

How do I conditionally disable Ramaze layout?


I'd like a controller method to respond by disabling/denying layout if the request happens to be an ajax request (i.e., request.xhr? == true).

The following doesn't seem to work -- it returns the layout nonetheless

class FooController < Ramaze::Controller
  layout :default

  def bar
    if request.xhr?
      layout nil
      "return something here"      #just return this string without the layout
    else
      ... #return with full layout
   end
end

Solution

  • You can bypass view and layout rendering by calling respond! like this :

    respond!(body, status, 'Content-Type' => 'whatever/foo')
    

    Where body is your (json ?) body, status the HTTP response code. You can then provide a hash with returned headers.

    However, you might be interested in provides that can handle requests differently if they end, for instance, with '.json'. See http://ramaze.net/documentation/file.views.html#View_Mapping for more info on this.