basic file upload in rails with form helper -


i'm trying head around basic file upload form helper in rails using edge rails guide (i'm using rails 4.0.0.rc1 , ruby 1.9.3p362) before paperclip or carrierwave.

i put method upload right in #create in controller, plan create new record :image column stored filename of upload.

my #create has this:

file.open(rails.root.join('public', 'uploads', uploaded_io.original_filename), 'w') |file|   file.write(uploaded_io.read) end 

when follow rails guide file upload happens, can see file in public/uploads, encoding error in browser:
encoding::undefinedconversionerror
"\x89" ascii-8bit utf-8

the \x89 dot on suffix of filename? ie: .png

i found stackoverflow post said add 'b' file.open, fixes encoding error, different error. stackoverflow post covers error, believe i'm following solution , still have error.

file.open(rails.root.join('public', 'uploads', uploaded_io.original_filename), 'wb') |file|   file.write(uploaded_io.read) end 

i following typeerror, no encoding error. on other hand, file uploads.

typeerror: can't cast actiondispatch::http::uploadedfile string: 

any suggestions i'm doing wrong? it'd nice see work before move paperclip or cw

i figured out using column :image upload, during uploading :image not :string, it's actiondispatch::http::uploadedfile object (right?). in database :image column field type :string. want store in :image uploaded_io.original_filename.

my first solution make work not use ':image' f.file_field in form_for. i'm calling :filename instead, grab uploaded_io.original_filename object , store in :image, save.

this ugly, works.

uploaded_io = params[:piture][:filename] file.open(rails.root.join('public', 'uploads', uploaded_io.original_filename), 'wb') |file|   file.write(uploaded_io.read) end @picture.image = uploaded_io.original_filename 

i'm still little hazy on file.write(upload_io.read) vs file.open(...)


Comments

Popular posts from this blog

SPSS keyboard combination alters encoding -

Add new record to the table by click on the button in Microsoft Access -

CSS3 Transition to highlight new elements created in JQuery -