# \[NEWBIE\] How to (pre-)process custom content types for model object creation?

**URL:** <https://www.ruby-forum.com/t/newbie-how-to-pre-process-custom-content-types-for-model-object-creation/149223>\
**Category:** Rails\
**Created:** [September 22, 2008, 5:31pm UTC](https://www.ruby-forum.com/t/newbie-how-to-pre-process-custom-content-types-for-model-object-creation/149223 "2008-09-22T17:31:10Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![FMFMFM](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/f08c70/32.png) [@FMFMFM](https://www.ruby-forum.com/u/FMFMFM)\
**Post date:** [September 22, 2008, 5:31pm UTC](https://www.ruby-forum.com/t/newbie-how-to-pre-process-custom-content-types-for-model-object-creation/149223/1 "2008-09-22T17:31:10Z")

</div>

Hi all,

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.

What’s the best way to achieve this?

Thanks and sorry if this is basic stuff.

Cheers,  
FM

---

<div class="post-metadata">

**Author:** ![FMFMFM](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/f08c70/32.png) [@FMFMFM](https://www.ruby-forum.com/u/FMFMFM)\
**Post date:** [September 23, 2008, 11:32am UTC](https://www.ruby-forum.com/t/newbie-how-to-pre-process-custom-content-types-for-model-object-creation/149223/2 "2008-09-23T11:32:57Z")

</div>

Hi everybody,

so I came out with the following solution:

1. I setup a before\_filter that checks if the submitted entity has the  
“application/atom+xml” content type
2. 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
3. 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.

Thanks.

-FM

---

<div class="post-metadata">

**Author:** ![FMFMFM](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/f08c70/32.png) [@FMFMFM](https://www.ruby-forum.com/u/FMFMFM)\
**Post date:** [September 23, 2008, 11:45am UTC](https://www.ruby-forum.com/t/newbie-how-to-pre-process-custom-content-types-for-model-object-creation/149223/3 "2008-09-23T11:45:52Z")

</div>

On 23 Sep 2008, at 10:32, FM wrote:

> 1. 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.

I think you could tidy this up a little - There is a way of telling  
rails ‘if the content-type is x then use this to parse the request  
parameters’

You just do something along the lines of

ActionController::Base.param\_parsers[some\_mime\_type] = Proc.new do |  
data|

# do something with data and turn it into a hash

end

---

<div class="post-metadata">

**Author:** ![FMFMFM](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/f08c70/32.png) [@FMFMFM](https://www.ruby-forum.com/u/FMFMFM)\
**Post date:** [September 23, 2008, 1:16pm UTC](https://www.ruby-forum.com/t/newbie-how-to-pre-process-custom-content-types-for-model-object-creation/149223/4 "2008-09-23T13:16:26Z")

</div>

On Sep 23, 11:44 am, Frederick C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> end

Thank you for the hint.  
It works!

… 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

…  
end

but it doesn’t work.

Thanks again for your help.

-FM

---

<div class="post-metadata">

**Author:** ![FMFMFM](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/f08c70/32.png) [@FMFMFM](https://www.ruby-forum.com/u/FMFMFM)\
**Post date:** [September 23, 2008, 1:42pm UTC](https://www.ruby-forum.com/t/newbie-how-to-pre-process-custom-content-types-for-model-object-creation/149223/5 "2008-09-23T13:42:15Z")

</div>

On 23 Sep 2008, at 12:15, FM wrote:

> > You just do something along the lines of  
> > … but now I have another question 🙂  
> > Is it possible to set this behavior on a per-controller basis?

Probably not - the method I gave is intended to be entirely general

Fred
