Hi all,
I know I can use ‘to_xml’ to get an xml representation of a model.
However, is there an easy built-in way to convert xml(for example coming
in as a REST web service call) back to a model? Or do I need to write my
own method for parsing it using REXML?
Ingo
I hope this makes sense. I am quite new to Rails, but have got my test
app working where the ‘input’ to a model Create or update is an XML
string. This XML only contains data relevant for the model, it is not a
web service.
The data magically appears in the params variable.
So if the xml looks like:
12345
20.99
500
This data will be in
params[:product][:id]
params[:product][:price]
params[:product][:quantityinstock]
The slight annoyance is accessing id for the model so I do
params[:id] = params[:product][:id]
This works for me and I use it to do updates, and create. If there is a
better way, I’d like to know?
I don’t know if this is what you are looking for.
Andrew
Thanks, Andrew!
I tried this, but it doesn’t seem to work for me. When my model XML is
POSTed, what you suggested let me expect to be able to do something like
this:
def create
Message.create(params[:product])
end
But that doesn’t work. The POST comes from a Flash XML class - maybe I
need to look into exactly how Flash sends the request…
Ingo
This is how I do my create
in class ProductsController
def create
product = Product.new(params[:product])
product.save
Are you using Flex or Flash?
Andrew