ruby on rails - Getting only file name as string -
i trying upload file in rails
i have created model following code
def self.save(upload,id) name = upload[:img].original_filename directory = "public/user_db" # create file path path = file.join(directory, id) # write file file.open(path, "wb") { |f| f.write(upload['img'].read) } end end
my view have following field.
<div class="field"> <input type="file" name="img" id="img" placeholder="upload dp" /> </div>
my controller calling save function following :
post = datafile.save(params,@fbuser.id)
do favor , don't reinvent wheel. in rails there 3 awesome gems handle file-uploading eatch having great community support , tons of shared code.
carrierwave https://github.com/carrierwaveuploader/carrierwave
paperclip https://github.com/thoughtbot/paperclip
dragonfly https://github.com/markevans/dragonfly
just follow instructions installation, migrate database, tell models how behave , lost headache within 5 minutes :-)
regarding problem-
name = upload[:img].original_filename
throws expection because upload[:img]
ist containing string. there no need .original_filename
but again - please use 1 of gems (or maybe read code idea of how do). there railscasts out there http://railscasts.com/episodes/253-carrierwave-file-uploads , http://railscasts.com/episodes/134-paperclip https://www.youtube.com/watch?v=gp_kn6afl-y
cheers
Comments
Post a Comment