I have a backbone app that I have integrated into my rails app. I am trying to pass my validation errors back to a Backbone Model on creation but it's not making it all the way back to Backbone.
Here's my model:
# app/assets/javascripts/models/source.js.coffee
class MyApp.Models.Source extends Backbone.Model
urlRoot: '/api/sources'
And my rails controller:
# app/controller/api/sources_controller.rb
class Api::SourcesController < ApplicationController
respond_to :json
def create
@source = Source.new source_params
if @source.save
respond_with @source
else
render json: { error: @source.error.messages }, status: 422
end
end
def source_params
params.require(:source).permit(:feed_id, :source, :term)
end
end
And finally the code that attempts to create my model:
# app/assets/javascripts/views/new_source.js.coffee
class MyApp.Views.SourceForm extends Backbone.View
events: ->
'submit .new_source': 'createSource'
createSource: ->
@source = new Juicer.Models.Source $(event.target).serializeHash()
@source.save
success: =>
console.log 'ya'
error: =>
console.log 'boo'
When I actually run this code however, it never makes it all the way back to this backbone callback, instead I get this error in the console:
POST http://localhost:3000/api/sources 422 (Unprocessable Entity)
I've dug around a bit in the Backbone source but I can't figure out why this isn't making it through. It seems to be stuck at the jQuery .ajax method, which should be triggering the error callback, but instead seems to be breaking.
I'm using Backbone v1.1.1 and jQuery v1.11.1 and Rails 4.1.5