I'm not sure if the following is intentional behaviour, or if not,
whether the problem lies in Sinatra or Rack.
You can name your form parameters 'foo[]' to build an array. However,
if there is exactly one parameter with this name, then you *don't* get
an array, but rather just a string. Example:
----------------
require 'rubygems'
require 'sinatra'
post '/' do
params.inspect + "\n"
end
----------------
$ curl -d 'post[foo][]=bar&post[foo][]=baz'
http://localhost:4567/
{"post"=>{"foo"=>["bar", "baz"]}}
$ curl -d 'post[foo][]=bar'
http://localhost:4567/
{"post"=>{"foo"=>"bar"}}
In the second case I was expecting to see
{"post"=>{"foo"=>["bar"]}}
This complicates controller code, as there are three cases to
consider:
- nil (nothing provided)
- string (one element provided)
- array (more than one element provided)
But Rails behaves how I expected:
----------------
class FooController < ApplicationController
def bar
render :text => params.inspect+"\n"
end
end
----------------
$ curl -d 'post[foo][]=bar'
http://localhost:3000/foo/bar
{"post"=>{"foo"=>["bar"]}, "action"=>"bar", "controller"=>"foo"}
[Need "config.action_controller.allow_forgery_protection = false"
for this to work with curl]
Regards,
Brian.