tags:

views:

150

answers:

3

Sinatra makes it easy to access any particular incoming form field by name:

post "/" do
  params['form_field_name']
end

But how does one enumerate over all the form fields in a request? I found nothing in the documentation. I even tried

request.body.split('&')

but request.body is an instance of StringIO, and not a string.

+2  A: 

If params is a hash, you can try:

params.keys.each do |k|
   puts "#{k} - #{params[k]}"
end
Geo
A: 

its just a hash :P so just iterate it like you would with any hash

TJ Holowaychuk
Yes, params is a hash - but it is "the union of GET and POST data" as the Rack API docs put it. I needed a way to find just the POST data, so the request.POST method is ideal.
davidstamm
A: 

I just discovered in Sinatra's excellent API docs that Sinatra::Request is a subclass of Rack::Request. The request object available to Sinatra handlers inherits has a POST method which returns a hash of submitted form fields.

request.POST.each { |k,v| puts "#{k} = #{v}" }
davidstamm