Creation of XML with objects

Hi, I am trying to get the response in xml format. For that I have written following code

def user_details     @user=User.find(params[:id])     @company=@user.company     respond_to do |format|       format.xml {render :xml=> @user.to_xml(:include => @company)}     end   end

I am getting following error:- "undefined method `macro' for nil:NilClass"

Can anyone tell, what is wrong with the code?

Thanks, Mike

Hi, I am trying to get the response in xml format. For that I have written following code

def user_details @user=User.find(params[:id]) @compa...@user.company respond_to do |format| format.xml {render :xml=> @user.to_xml(:include => @company)}

you should be giving the name of the association - not the actual associated object

Fred

Hi Fred, How can I create user defined xml structure?

My previous code gives me xml like: <user> <name>Mike></name> <email>mike@yahoo.com</email> <company>    <id>2</id>    <name>ABC</name> </company> </user>

I need to create a XML like this:-

<user>     <name>Mike</name>     <email>mke@yahoo.com</email>     <company_id>2</company_id> </user>

Any help appreciated.

Thanks, Mike

If the default to_xml isn't giving you want you want you can override it yourself (to_xml does take quite a few options though - have a play with them)

Fred

Hello,

if you remove the :include => :company only the company_id will be serialized.

The following code should work as you expect : render :xml => @user

Mickael

Hi, I need the "company_id" in the XML.

render :xml=> @user will only include the user attributes.

Thanks, Mike

Hi, I need the "company_id" in the XML.

render :xml=> @user will only include the user attributes.

Is the assocation user has_one company ? If so (ie user has no company_id attribute) then the easiest thing is probably to write a method on user that returns the appropriate id and then use the :methods option to to_xml to include that in the output

Fred