views:

935

answers:

2

I'm running the latest Rails 2-3-stable branch (currently 2.3.3).

I'm using JQuery to post an AJAX request to my 'create' action, in which I have the following block:

respond_to do |format|
  format.js
end

I have created create.js.erb and to test this action, I've added the following single line:

alert('hello');

The request correctly enters the format.js block, but the response attempts to render a layout. Here's my log:

Jul 22 20:44:27 [2970] INFO: Rendering template within layouts/application
Jul 22 20:44:27 [2970] INFO: Rendering contacts/create

If I change my respond_to block to the following, it works:

respond_to do |format|
  format.js { render :layout => false }
end

Is this expected behaviour or is this a bug in Rails? I would have thought the fact that I'm rendering a JS response would be enough to set layout to false.

+1  A: 

My memory of Ajax on Rails books is that this was the standard if not necessarily expected behaviour in earlier editions of rails.

The following ticket shows the bug logged a few versions back, as well as a way to define the behaviour as default.

http://dev.rubyonrails.org/ticket/3229

Sadly the description of what the later changes are that make this obsolete are not explained in the final comment.

Greg
A: 

The JQuery function which makes the Ajax POST request should return "false". Check if you are doing the same. It should be implemented like following:

postFormData: function () {
  $('#form_id').submit(function () {
    $.post($(this).attr('action'), $(this).serialize(), null, 'script');
    return false;
  });
}

Notice the last line which returns false.

Waseem
Yes, I'm returning false already
Olly