# File upload

**URL:** <https://www.ruby-forum.com/t/file-upload/173030>\
**Category:** Rails\
**Created:** [July 31, 2009, 6:00pm UTC](https://www.ruby-forum.com/t/file-upload/173030 "2009-07-31T18:00:11Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![archangel77](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/6a8cbe/32.png) [@archangel77](https://www.ruby-forum.com/u/archangel77)\
**Post date:** [July 31, 2009, 6:00pm UTC](https://www.ruby-forum.com/t/file-upload/173030/1 "2009-07-31T18:00:11Z")

</div>

Hello. I’m new with Rails and I’m trying to create a file upload app.

I want to make that it stores the file in public/data, that only .swf  
files can be uploaded and that the filename in my server is the same  
from the form (see below).

I currently have these files and want to integrate the upload to that  
form, but I don’t know how to do that.

Thank you,  
Gabriel.

* * *

class SfilesController \< ApplicationController  
def new  
@sfile = Sfile.new  
end

def create  
@sfile = Sfile.new(params[:sfile])  
respond\_to do |format|  
if @sfile.save  
flash[:notice] = ‘File was successfully created.’  
format.html { redirect\_to(@sfile) }  
else  
format.html { render :action =\> “new” }  
end  
end  
end

def show  
@sfile = Sfile.find(params[:id])  
end  
end

* * *

class Sfile \< ActiveRecord::Base  
validates\_presence\_of :title  
end

* * *

# Welcome

\<% form\_for(@sfile) do |f| %\>  
\<%= f.error\_messages %\>

\<%= f.label :title %\>  
 \<%= f.text\_field :title %\>

\<%= f.label :description %\>  
 \<%= f.text\_area :description %\>

\<%= f.submit 'Create' %\>

\<% end %\>

---

<div class="post-metadata">

**Author:** ![archangel77](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/6a8cbe/32.png) [@archangel77](https://www.ruby-forum.com/u/archangel77)\
**Post date:** [July 31, 2009, 9:05pm UTC](https://www.ruby-forum.com/t/file-upload/173030/2 "2009-07-31T21:05:09Z")

</div>

hum, the question is the file it’s represented in a model?anyway, i used  
virtual attributes…  
any questions, just ask =)

at sfile.rb

validate\_on\_create :validate\_upload

def temp\_file= temp\_file  
@temp\_file = temp\_file  
end

protected

def validate\_update  
errors.add\_to\_base(“Upload a valid Flash file”) unless  
validate\_type\_file @temp\_file  
end

def validate\_type\_file file  
original\_filename = file.original\_filename  
extension = SFile.ret\_ext\_file original\_filename  
reg\_file = Regexp.new(/(flw)/)  
if n =~ reg\_file  
return true  
else  
return false  
end  
end

def self.ret\_ext\_file n  
v = n.split(‘.’)  
v = v[v.size-1].downcase  
return v  
end

at .erb

# Welcome

\<% form\_for(@sfile) do |f| %\>  
\<%= f.error\_messages %\>

\<%= f.label :title %\>  
 \<%= f.text\_field :title %\>

\<%= f.label :description %\>  
 \<%= f.text\_area :description %\>

\<%= file\_field 'i', 'temp\_file' %\>

\<%= f.submit 'Create' %\>

\<% end %\>

at sfile\_controller.rb

def create  
@sfile = Sfile.new(params[:sfile])  
@sfile.attributes = params[:i]  
respond\_to do |format|  
if @sfile.save  
flash[:notice] = ‘File was successfully  
created.’  
format.html { redirect\_to(@sfile) }  
else  
format.html { render :action =\> “new” }  
end  
end  
end

2009/7/31 Gabriel B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)
