views:

11

answers:

1

I'm attempting to render xml from a service application.

In the model, the relationship is defined as:

class Customer < ActiveRecord::Base
    has_many :licenses
    accepts_nested_attributes_for :licenses
end

In my Controller, I have the following code:

if @customer.save
    render :xml => @customer, :status => :created
else
    render :xml => @customer.to_xml(:include => [:errors, :licenses]), :status => :unprocessable_entity
end

But this fails with a NoMethodError: undefined method `macro' for nil:NilClass error.

On the console, I can replicate the error simply enough:

>params = {...}
>c = Customer.new(params)
>c.save
=> false
>c.errors
=> #<ActiveRecord::Errors::...
>c.to_xml(:include => :errors)
NoMethodError: undefined method `macro' for nil:NilClass
... Stack Trace ...
>c.errors.to_xml
=>XML Showing Errors

I know that I can render errors to xml easily enough as:

render :xml => @customer.errors

But, I'd really like to be able to return both the errors and this other collection. Maybe someone can shed some insight on why I get this error.

A: 

it's not include but :methods you need use

render :xml => @customer.to_xml(:include => [:licenses], :methods => [:errors]), :status => :unprocessable_entity
shingara
Thanks, this works a charm.
idbentley