title: Rails prevent layout during AJAX request
body: <p>I have searched around and have not been able to find a solution for this type of mechanic.  I want to load all pages normally in Rails, but whenever I do an ajax request I just want to return the page without the layout.  So anytime I make an ajax requst I can append a <code>?page=true</code> or something along those lines and have Rails just return the page without the layout.</p>
  
  <p>Is this possible?  Is there a better way to do it that I am missing?</p>
  
  <p>Thanks for any help!</p>
  
  <p><strong>Final Solution Working Code:</strong></p>
  
  <p>In the controller all you need to do is append a little logic to the <code>format.html</code> in the <code>respond_to</code> block.</p>
  
  <p>In the <code>show</code> method for example</p>
  
  <pre><code>def show
      # code beforehand
  
      respond_to do |format|
          format.html { render :layout =&gt; !request.xhr? }
          # other formats
      end
  end
  </code></pre>
  
  <p>And that's it!  Prevent layouts during AJAX requests!</p>
  
  <p><strong>Note:</strong> Thanks to the smathy's comment on his answer this was simplified further.  I originally had <code>format.html { render :layout =&gt; nil if request.xhr? }</code>  This solution works just as well, but smathy's modification keeps it even simpler.</p>