Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does one access all form fields in Sinatra?

Tags:

ruby

sinatra

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.

like image 785
davidstamm Avatar asked Oct 30 '09 21:10

davidstamm


2 Answers

If params is a hash, you can try:

params.keys.each do |k|
   puts "#{k} - #{params[k]}"
end
like image 186
Geo Avatar answered Nov 15 '22 02:11

Geo


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}" }
like image 41
davidstamm Avatar answered Nov 15 '22 02:11

davidstamm