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

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

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

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.

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

Thank you for the hint.
It works!

... but now I have another question :slight_smile:
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

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

Thank you for the hint.
It works!

... but now I have another question :slight_smile:
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