Forum: Ruby on Rails RESTful parameters - Why input parameters are so "webish" ?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Eb778b8bb642aa8a82c2b8b63705b6af?d=identicon&s=25 Jean-Etienne Durand (jetienne)
on 2007-04-21 16:20
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
821395fe70906c8290df7f18ac4ac6cf?d=identicon&s=25 Rick Olson (Guest)
on 2007-04-21 17:26
(Received via mailing list)
On 4/21/07, Jean-Etienne Durand <rails-mailing-list@andreas-s.net>
wrote:
>   respond_to |format|
>   ...
> 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
Eb778b8bb642aa8a82c2b8b63705b6af?d=identicon&s=25 Jean-Etienne Durand (jetienne)
on 2007-04-21 19:16
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,
This topic is locked and can not be replied to.