Skip to content
This repository

Ajax callbacks in Rails 3.1.1.rc1 are broken (as demo'd at GoGaRuCo) #3059

sgharms opened this Issue September 17, 2011 · 9 comments

4 participants

Steven G. Harms José Valim Guillermo Iguaran Vijay Dev
Steven G. Harms

Use: Rails 3.1.1.rc1

  1. rails new twooter
  2. cd twooter
  3. rails generate scaffold Twoot author:string message:string
  4. bundle exec rake db:migrate
  5. vi app/views/twoots/_form.html.erb
    1. Add: :remote=> true to the form_for(@twoot) statement
    2. Add a div for the ajax callback: <div id="update"></div>
  6. change from .json to .js in the twoots_controller.rb: format.js { render json: @twoot, status: :created, location: @twoot }
  7. Edit app/assets/javascripts/
     $(document).ready ->
         .bind "ajax:success", (event, data) ->
           $("#update").append("Some ajaxy text")
  1. Visit twoots/new
  2. Ajax event will fire, but no change
  3. Alter format.js { render json: @twoot, status: :created, location: @twoot} to format.js { render json: @twoot, status: :created, location: @twoot, content_type: 'application/json'}
  4. Re-submit your twoot
  5. Works!

Basically you have to pass content_type: 'application/json' for AJAX to callbacks trigger.


  1. The scaffold is wrong?
  2. Maybe we're supposed to be passing content_type: 'application/json?

I'm working to assemble a test case to demo that. Maybe someone here recognizes this error already?

José Valim

Step 6 should only be "format.js" without the extra stuff.

Steven G. Harms

Per josevalim:

  1. Changed render_to to be: format.js
  2. Re-submitted #=> 500 Internal Server error:
Template is missing
Missing template twoots/create, application/create with {:handlers=>[:erb, :builder, :coffee], :formats=>[:js, :html], :locale=>[:en, :en]}. Searched in: * "/Users/sgharms/breakajax/app/views"
  1. Created create.js.erb with a simple alert(), re-submitted #=> Success!
  2. Ajax event defined in in executes properly


  1. Adding any {} after the format.js causes the execution of create.js.erb to be ignored.
  2. Coffeescript assets are only successful when passing content_type, per original complaint
  3. Is this correct behavior?

Edge case

  1. Take code and migrate it into create.js.erb.
  2. Revert to use format.js only line
  3. JS fires
  4. Putting anything in {} fails to execute create.js.erb


  1. Anything in {} after format.js statement is ignored
  2. Code can be set in either coffescript home OR in the local $action.js.erb

Jose: Is that the right behavioral model? If you can validate this and point me to the canonical doc site I will add this to the reference material.

Guillermo Iguaran

do you have same result using 3.1.0?

Steven G. Harms

Guillermo: Yes, I have just reproduced this on 3.1.0 as well. The fix is the same.

José Valim
  1. Yes, at the moment you put something inside format.js { } you are telling it that you are specifying your own behavior, instead of using the default which is to render a template.

  2. If you want to render coffee, the view needs to be create is the name of the action, js is the content type and coffee is the template handler (what renders the source template and gives you an output, in this case, pure JS).

José Valim josevalim closed this September 20, 2011
José Valim

About docs: I don't know if we have an AJAX guide. But you can have more information about how respond_to and format.js works by checking respond_to docs.

Vijay Dev

We do have an AJAX guide, but it is incomplete and outdated :(

Steven G. Harms

@Vijay: Do you have a URL whose content I can edit?
@José: Thank you, as always.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.