Upload File in Ruby on Rails

 __                          __             
|__)     |_        _   _    |__)  _  . |  _ 
| \  |_| |_) \/   (_) | )   | \  (_| | | _) 
             /                              

– UPLOAD FILE

tutorial singkat ini menjelaskan bagaimana melakukan upload file menggunakan rails

versi yang gw pake:

>> Rails::VERSION::STRING

=> “2.2.3″

>> RUBY_VERSION

=> “1.8.7″

>>

langsung …

- create new project

    rails uploads

- atur config

- generate controller

    ./script/generate controller uploads index new save
    # script ini akan menghasilkan controller uploads dengan action "index", "new",
    # dan "save", and ga lupa menghasilkan view juga pastinya

- generate model

    ./script/generate model upload

- edit dikit migrate nya …

    vim db/migrate/20100403135954_create_uploads.rb
    # edit aja fungsi create_table
    def self.up
      create_table :uploads do |t|
        t.string  :name
        t.timestamps
      end
    end
    #nambah field name, tipe string
    rake db:migrate

- edit action yang ada di controller Uploads

1. index

    def index
        @upload = Upload.find(:all)
    end
    # variable @upload menyimpan semua isi yang diambil dari table upload
    # Upload.find(:all), ini menggunakan active recordnya rails, intinya select semua
    # yang ada di table upload, tanpa ada filter sama sekali (:all)

2. new

    def new
        @upload = Upload.new
    end
    # deklarasi @upload sebagai object baru

3. save

    def save
        uploaded = params[:upload][:name]
        @upload = Upload.new(:name => uploaded.original_filename)
        respond_to do |format|
            if @upload.save
                File.open(Rails.root+'/public/uploads/'+uploaded.original_filename, 'w') do |file|
                    file.write(uploaded.read)
                end
                flash[:notice] = 'Post success.'
                format.html { render :text => "success" }
            else
                format.html { render :action => "new" }
            end
        end
    end
    # uploaded, mengambil isi variable form upload dengan field name
    # hasil post form di simpan kedalam folder public/uploads
    # uploaded.original_filename, artinya mengambil raw name dari file yang diupload.
    # selain original_filename, ada juga content_type dan original_path

- edit views

1. index

<table border="0">
<tbody>
<tr>
<th>No</th>
<th>Name</th>
<th>Date</th>
</tr>
&lt;% no=0 %&gt;
        &lt;% for upload in @upload %&gt;
<tr>
<td>&lt;%= no +=1 %&gt;</td>
<td>&lt;%=h upload.name %&gt;</td>
<td>&lt;%=h upload.created_at %&gt;</td>
</tr>
&lt;% end %&gt;</tbody></table>
# semua hasil dari @upload (Upload.find(:all)), diloop
    # tag &lt;%= artinya memanggil value dari variable tersebut
    # jika tag &lt;%=h value dipanggil dan memangkasnya, menghindari xss dan sejenisnya

2. new

    &lt;% form_for :upload, @upload, :url=&gt;{:action =&gt; "save"}, :html =&gt; {:multipart =&gt; true} do |f| %&gt;
        &lt;%= f.file_field :name %&gt;
        &lt;%= f.submit "Upload" %&gt;
    &lt;% end %&gt;
    # form dibuat menggunakan formhelper rails
    # untuk upload, penting mengenable parameter :multipart

udah selese, moga bermanfaat …

kalo ada yang salah… tolong di kasi tau .. maklum nubi …

sumber: http://guides.rubyonrails.org

Share this Post:
Digg Google Bookmarks reddit Mixx StumbleUpon Technorati Yahoo! Buzz DesignFloat Delicious BlinkList Furl

No Responses to “Upload File in Ruby on Rails”

Leave a Reply:

Name (required):
Mail (will not be published) (required):
Website:
Comment (required):
XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="">