codebrew / backbone-rails

Easily use backbone.js with rails 3.1
MIT License
1.62k stars 255 forks source link

save() triggering error, but no noticeable error given #90

Closed unclaimedbaggage closed 12 years ago

unclaimedbaggage commented 12 years ago

Hi folks,

I have the following code in a backbone-rails view:

$(@el).droppable({
  hoverClass: 'hoverTask',
  appendTo: "body",
  drop: (event, ui) =>
    ...
    task_model = @options.tasks.get(task_id)
    task_model.save({due: due_date}, {
      success: () =>
        console.log 'it was successful'
      error: () =>
        console.log 'error'
    })

The problem is, the save is triggering error() every time, despite the rails side of thing saving successfully, the backbone side of things updating successfully, and no error being thrown in either the rails logs or the browser console. Any ideas what could be going on here?

winfred commented 12 years ago

(going out on a limb here, but I've been frustrated by the exact same thing) change your controller response to have some JSON rather than just "head: ok" (the rails default for contrller#update)

respond_to do |format|
  format.json {render json: @task}
end

When expecting JSON, jQuery calls the error event if something that isn't JSON is returned, such as an empty body.

Rails vs. jQuery. FIGHT! http://forum.jquery.com/topic/jquery-calls-error-callback-on-successful-ajax-request-status-200

unclaimedbaggage commented 12 years ago

Ahh - of course. Many thanks, much appreciated.