I have the following scenario: I scaffolded a resource R and in the
generated create method I have the code that responds to html and xml.
Basically by POSTing to the URI I can create entities either via forms
or via XML (where basically the tags have the same names of the
underlying model’s fields)
The create method’s body does this: @r = R.new(params[:r]), and it
works because Rails magically populates the params hash with the data
coming from the request (i.e., from the html form’s field or from the
XML content)
Now the question is: what if I want to use another format to
communicate this data?
In particular, what if I want to use an ATOM entry to tell the web app
what data I want to put into the newly created resource of type R?
In this case I would need to transfer the information contained in my
ATOM entry XML to the params has so that everything will work
correctly.
I setup a before_filter that checks if the submitted entity has the
“application/atom+xml” content type
If this is the case, I parse the entity and I populate accordingly
the params hash in order to map the information contained in the atom
entry to my model
I put respond_to atom where needed in order to handle atom entry
submissions.
Is this solution good?
I think it is, but since I am a newbie to Rails, I would appreciate
some experts’ opinions.
… but now I have another question
Is it possible to set this behavior on a per-controller basis?
What I mean is that I would like to parse atom only in the context of
some specific controllers.
So I tried this:
class MyResourceController < ApplicationController
@@param_parsers[Mime::ATOM] = lambda do |body|
…do the processing here to translate the atom entry to an hash
matching my resource’s model
end