Jean-Etienne Durand
2007-Apr-21 14:20 UTC
RESTful parameters - Why input parameters are so "webish" ?
Hi, Using Rails 1.2.3, I would like to know if it is possible to set the format of parameters for a POST call. Like you have: # GET /assets/# def show @asset = Asset.find(params[:asset]) respond_to |format| format.html format.xml {render :xml => @asset.xml} end end why the format of params[:asset] in # POST /assets def assets @asset = Asset.new(params[:asset]) ... end needs to have a very "webish" format: "asset[title]=foo&asset[description]=bar" ? Why not xml/json? Is it possible to have a prefilter to accept xml, json ...? Or better, formatting the input parameters according to Accept header value? If it is not supported, how would you intercept the parameters to convert them from xml/json to this array format ? Regards, Jean-Etienne -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Rick Olson
2007-Apr-21 15:25 UTC
Re: RESTful parameters - Why input parameters are so "webish" ?
On 4/21/07, Jean-Etienne Durand <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> > Hi, > > Using Rails 1.2.3, I would like to know if it is possible to set the > format of parameters for a POST call. > > Like you have: > # GET /assets/# > def show > @asset = Asset.find(params[:asset]) > respond_to |format| > format.html > format.xml {render :xml => @asset.xml} > end > end > > why the format of params[:asset] in > > # POST /assets > def assets > @asset = Asset.new(params[:asset]) > ... > end > > needs to have a very "webish" format: > "asset[title]=foo&asset[description]=bar" ? Why not xml/json? Is it > possible to have a prefilter to accept xml, json ...? Or better, > formatting the input parameters according to Accept header value? If it > is not supported, how would you intercept the parameters to convert them > from xml/json to this array format ?Sure can! You can post XML (the same format as the @asset.to_xml output) if you set the content-type to application/xml, or append a .xml extension. ActiveResource works like this. There'' s a param parser for Mime::XML that parses the xml and returns a hash in the same format, so your controller code doesn''t need to change. All you need is a respond_to block for the response. I have an experimental plugin for JSON support: http://svn.techno-weenie.net/projects/plugins/json_for_rails, but there are a few issues: * It uses the fjson gem (fast json parser written in c). I seem to recall rails has this now, buy converting json to YAML and returning that. * json has no date/time literal. If you look at my plugin, it looks for any strings in the xmlschema format and parses them to date/times. But, this is a hack. Currently there''s no standard for times in json, so I don''t really think json is suitable for this task. You can come up with your own convention if you like, or just use XML and get on with your app. * set up a simple json param parser. This would be a good guide if you wanted a simple way to process other common formats, for example. -- Rick Olson http://lighthouseapp.com http://weblog.techno-weenie.net http://mephistoblog.com --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Jean-Etienne Durand
2007-Apr-21 17:16 UTC
Re: RESTful parameters - Why input parameters are so "webish
Hi, This is what I tought at the beginning, I tried to pass the xml I got from a GET (application/xml, not assets.xml) but with no luck. Checking the logs, the xml is understood as a simple string... I remind you I am not working on Rails... Regards, -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---