Backbone.js and handle error messages from rails?

I wonder if backbone.js users can help me?

What is the best way to encode error messages from a rails application when used with backbone.js, for example, error messages that were once identified as flash messages, such as "record not found".

In most cases, errors can be detected in the client, but sometimes you want to pass the error defined in the code on the server side, which means that the result from the server is different from what is expected from the usual receipt of the list of records in the collection.

+5
source share
1 answer

:

reply_to: json

json ( response_with (object))

class XYZController < ApplicationController
  respond_to :html, :json
  responders :jsons
  def create
    @xyz = Xyz.new( params[:xyz] )
    @xyz.save
    respond_with @xyz, :location=>@xyz.id.nil? ? "" : edit_xyz_url(@xyz)
  end
end

json, :

module Responders

  module JsonResponder 

    def to_json
      raise error unless resourceful?

      if get?
        display resource
      elsif has_errors?
        display resource.errors, :status => :unprocessable_entity
      elsif post?
        display resource, :status => :created, :location => api_location
      elsif put?
        display resource, :status=>:ok, :location => api_location
      elsif has_empty_resource_definition?
        display empty_resource, :status => :ok
      else
        head :ok
      end
    end
  end
end
+4

All Articles