Sebastian
Sebastian

Reputation: 3399

as_xml in model not working

I want to respond with json and xml in my ruby on rails app. In my controller (e.g "Person") I have:

respond_to :html, :json, :xml

In the show-method:

@person = Person.find(params[:id])
respond_with @person

In my Person-Model I define a 'as_json' and a 'as_xml' method, because I want to include data.

def as_json(options={})
  super(:include => :parents)
end
def as_xml(options={})
  super(:include => :parents)
end

The call /persons/1.json is correct. But the call /persons/1.xml gives me only the person attributes as an xml. The include is missing.

I can't find informations for xml, only for json. Is it possible to use as_xml?

Upvotes: 0

Views: 959

Answers (2)

Brian
Brian

Reputation: 2439

Like lucapette already suggested, you might want to use to_xml

You can do something like this in your model to get custom xml (or json) output

def to_xml options = {}
  return generate_output_object.to_xml options
end

def generate_output_object
  return {"myobject" => {"special_processing" => get_special_data}}
end

Upvotes: 0

lucapette
lucapette

Reputation: 20724

Maybe you are searching for to_xml

Upvotes: 1

Related Questions