FM
2008-Sep-22 15:09 UTC
[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 --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
FM
2008-Sep-23 09:32 UTC
Re: How to (pre-)process custom content types for model object creation?
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 --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Frederick Cheung
2008-Sep-23 09:44 UTC
Re: How to (pre-)process custom content types for model object creation?
On 23 Sep 2008, at 10:32, FM wrote:> > 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> Thanks. > > -FM > >--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
FM
2008-Sep-23 11:15 UTC
Re: How to (pre-)process custom content types for model object creation?
On Sep 23, 11:44 am, Frederick Cheung <frederick.che...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > 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 :) 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 --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Frederick Cheung
2008-Sep-23 11:41 UTC
Re: How to (pre-)process custom content types for model object creation?
On 23 Sep 2008, at 12:15, FM wrote:> > > > On Sep 23, 11:44 am, Frederick Cheung <frederick.che...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> > wrote: >> >> 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 :) > 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> > 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 > >--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---