Skip to content

Instantly share code, notes, and snippets.

@vangberg
Created February 21, 2009 16:47
Show Gist options
  • Select an option

  • Save vangberg/68085 to your computer and use it in GitHub Desktop.

Select an option

Save vangberg/68085 to your computer and use it in GitHub Desktop.
If only one of the text fields are filled in the params will look like this:
{"foo" => "bar"}
While it looks like this if both are filled in:
{"foo" => ["bar", "baz"]}
And really, in the first case it *should* be:
{"foo" => ["bar"]}
While there might be some debate over this, I think it's pretty natural:
The code that handles the form data expects params["foo"] to be an array.
With the current behaviour that code needs to check wether params["foo"]
is an array or something else, and then act differently depending. If
params["foo"] always is an array, the same piece of code handles it all.
Less hassle.
require 'rubygems'
require 'sinatra'
get '/' do
puts params.inspect
haml :index
end
__END__
@@ index
!!!
%html
%body
%form{:action => '/', :method => 'GET'}
%input{:type => 'text', :name => 'foo[]'}
%input{:type => 'text', :name => 'foo[]'}
%input{:type => 'submit'}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment