Sneaky Abstractions

Subscribe to my Feed, follow me on , recommend me on Working With Rails or see my code on GitHub

ActsAsFile

API SVN

ActsAsFile is a Rails plugin which saves (uploaded) files to the file system.

Installing

Download the plugin from Github and put it in your vendor/plugins directory.

Using

class Upload < ActiveRecord::Base

  acts_as_file

  self.save_path = File.join(RAILS_ROOT, 'public', 'uploads')
  self.read_path = 'uploads'

end
class UploadsController < ActionController::Base

  def create
    upload = Upload.new(params[:upload])
    
    if upload.save
      flash[:notice] = 'File saved'
      redirect_to upload_url(upload)
    else
      flash[:error] = 'Could not save file'
      redirect_to new_upload_url
    end
  end

end

Comments

83 comments

Ice Ice Baby