# File upload

**URL:** <https://www.ruby-forum.com/t/file-upload/143418>\
**Category:** Rails\
**Created:** [July 18, 2008, 10:36am UTC](https://www.ruby-forum.com/t/file-upload/143418 "2008-07-18T10:36:17Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![jaiganez](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/3ec8ea/32.png) [@jaiganez](https://www.ruby-forum.com/u/jaiganez)\
**Post date:** [July 18, 2008, 10:36am UTC](https://www.ruby-forum.com/t/file-upload/143418/1 "2008-07-18T10:36:17Z")

</div>

i cant able to insert filename into database…pl;s help

my

model.rb  
class Mobile \< ActiveRecord::Base  
validates\_presence\_of:image

validates\_presence\_of:brand  
def mobile=(picture\_field)  
self.image = picture\_field[‘image’].respond\_to?(:original\_filename)  
? base\_part\_of(picture\_field.original\_filename) : nil

end  
def self.save(product)  
name=product[‘image’].original\_filename  
path = File.join(“public/data”, name)  
File.open(path, “wb”) { |f| f.write(product[‘image’].read) }

end  
end  
…  
controller.rb

def create  
@mobile=Mobile.new(params[:mobile])  
if @mobile.save  
post=Mobile.save(params[:mobile])  
redirect\_to :action=\>‘list’  
else  
render :action=\>‘new’  
end  
end  
…

new.rhtml

\<%= error\_messages\_for ‘mobile’ %\>  
\<% form\_tag ({ :action =\> ‘create’ },  
{ :multipart =\> ‘true’ }) do%\>

\<%=text\_field 'mobile','brand'%\>

\<%=file\_field 'mobile','image'%\>

\<%=submit\_tag 'Submit'%\>

\<%end%\>
