Hi all,
Has anyone successfully used WWW::Mechanize with file_column? I’m
trying to upload a bunch of images to a custom controller, and
something’s going wrong with the parameter names.
Using a noddy controller :create action that looks like this:
def create
render :text => params.inspect
end
I get this result from a manual upload from a form:
{“commit”=>“Create”, “snapshot”=>{“screenshot”=>#<File:/tmp/CGI8585.3>,
“screenshot_temp”=>"", “processes”=>“foo bar”}, “action”=>“create”,
“controller”=>“snapshots”}
(model name is Snapshot which has a file_column :screenshot, controller
is :snapshots)
But I get this result from Mechanize:
{“snapshot%5Bscreenshot%5D”=>#<File:/tmp/CGI8585.4>, “action”=>“create”,
“controller”=>“snapshots”, “snapshot%5Bprocesses%5D”=>"",
“snapshot%5Bscreenshot_temp%5D”=>""}
It looks to my untutored eye like the query keys are getting
double-quoted on upload. Anyone else seen this, or anything like it?
The code driving mechanize looks like this:
img = @form.file_uploads.find{|f| f.name == ‘snapshot[screenshot]’}
img_name = time + ‘.png’
img.file_data = File.read(File.join(dirname, img_name))
img.file_name = img_name
img.mime_type = ‘image/png’
@form.fields.find{|f| f.name == ‘snapshot[processes]’}.value = ‘’
response = @agent.submit(@form)
puts response.body
@form is found from @agent.get(blah).forms.first, @agent is a
WWW::Mechanize.new.
I’m stumped - I can’t see anything that looks wrong here.