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])
@[email protected]
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

On Aug 9, 11:15 am, Mike D. [email protected] wrote:

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])
@removed_email_address@domain.invalid
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:

Mike>
[email protected]

2
ABC

I need to create a XML like this:-

Mike [email protected] 2

Any help appreciated.

Thanks,
Mike

On Aug 9, 12:00 pm, Mike D. [email protected] wrote:

I need to create a XML like this:-

Mike [email protected] 2

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

On Aug 9, 2:16 pm, Mike D. [email protected] wrote:

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

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs