Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to render a RABL template for errors instead of the standard JSON hash #222

Closed
burlesona opened this issue Apr 16, 2012 · 1 comment
Closed
Labels

Comments

@burlesona
Copy link

Sorry to post this as an issue but I have not been able to find any useful Google results on this nor get help on SO etc. I think this is a fairly common question so I hope you can simply point me to the documentation I'm failing to find...

Using RABL in Rails 3.2.x, given the following controller action:

respond_to :html, :json

def create
  @foo = Foo.create(params[:foo])
  respond_with @foo
end

Assuming the validation fails, how do you get respond_with to use a RABL template instead of the standard JSON hash of errors?

IE. The current response would be something like: { errors: { error: { base: "Record is invalid." } } }

I would prefer the response to use my create.json.rabl template to return the complete @foo object including an errors node.

I tried duplicating the create template and naming it show and new but neither of those worked. So what am I missing here?

@databyte
Copy link
Collaborator

With a basic model:

class Foo < ActiveRecord::Base
  attr_accessible :kitten, :unicorn, :rainbow

  validates_presence_of :unicorn
end

I was able to get this output:

$ curl -H "Accept: application/json" -H "Content-type: application/json" -X POST -d '{"foo":{"kitten":"please"}}' http://localhost:3000/foos
{"errors":{"unicorn":["can't be blank"]}}

$ curl -H "Accept: application/json" -H "Content-type: application/json" -X POST -d '{"foo":{"kitten":"please","unicorn":"exist"}}' http://localhost:3000/foos
{"foo":{"kitten":"please","unicorn":"exist","rainbow":null,"errors":{}}}

using the following template:

# create.rabl

object @foo
extends "foos/foo"

# foo.rabl

attributes :kitten, :unicorn, :rainbow

node :errors do |o|
  o.errors
end

In order to get customize the return when an error occurs, I did the following:

  def create
    @foo = Foo.create(params[:foo])
    respond_with @foo do |format|
      format.json { render 'create' }
    end
  end

which gives you:

$ curl -H "Accept: application/json" -H "Content-type: application/json" -X POST -d '{"foo":{"kitten":"please"}}' http://hades.local:3000/foos
{"foo":{"kitten":"please","unicorn":null,"rainbow":null,"errors":{"unicorn":["can't be blank"]}}}

Read up more about it in the ActionController's Responder under the Custom Options section at the top. It's just the way Rails handles errors given the default JSON responder. Nothing RABL specific.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants