Rails: Custom Responder

682 Views Asked by At

Inside my application there is a jQuery autosave library that upon a user tabbing out of a field, the field is sent to the backend via AJAX.

The structure of my JSON response is like so:

render json: { message: @object.errors.any? ? 
                          @object.errors.full_messages.to_sentence : 
                          "Updated Successfully" }

Most of all of the admin section is responding with JSON and was curious to know (using the new Rails 3 respond_with syntax) how to implement a custom responder that would automatically create the JSON object for me and then I can simply write respond_with @object

1

There are 1 best solutions below

0
On

You can use jBuilder which will give you incredible flexibility to create JSON views (if you like ;) ). Check it out here https://github.com/rails/jbuilder

jBuilder has many alternatives. On popular alternative is RABL