I’m trying to do a simple file upload page but keep getting “can’t convert nil into Hash” (file: request.rb location: merge line: 221). This happens on Windows and Ubuntu. Ruby 1.9.3, Sinatra 1.3.2. What I also notice is that POST data is apparently empty, and regardless of the size file I try to upload the request is always just under 70k.
The handler isn’t doing much:
get "/upload" do
erb :upload
end
post '/upload' do
tempfile = params['file'][:tempfile]
filename = params['file'][:filename]
File.copy(tempfile.path, "./files/#{filename}")
redirect '/'
end
the form looks like:
<div id="bodydiv">
<h1>file upload page</h1>
File to upload:
<form method="post" action="" enctype="multipart/form-data">
<input type="file" id="file" />
<input type="hidden" value="tokenstuff" id="hiddenThing" />
<input type="submit" value="upload" id="commit" />
</form>
</div>
You have to add a
nameattribute to your file input.I don’t why though, if someone can explain this. Thanks.