Acts As File plugin
Plugin details
Documentation
ruby script/plugin install git://github.com/toretore/acts_as_file.git
== Model
If you haven't created your model yet, you can use the generator to create a model and a migration that can be used with ActsAsFile:
script/generate acts_as_image model Upload
Then, migrate to create the new table:
rake db:migrate
If you have an existing model that you want to use, make sure it has the field filename. The fields content_type, size and original_filename will also be used if present. The plugin will create a virtual attribute file on the model that will be used to hold the file before it is written to disk. Finally, add acts_as_file to the model class definition.
== Controller and view
You will have to add the uploaded file to the model's file attribute before saving it. You can have this done automatically by making sure file is accessible to mass assignment and naming the field in the form model_name[file].
class UploadsController < ActionController::Base def create upload = Upload.new upload.file = params[:upload][:file] if upload.save #success else #ActsAsFile will add validation errors if the file can't #be saved for some reason. A new model instance which acts_as_file #must have the file attribute set. end end end
== Paths
You can control which path the files will be saved to by setting the save_path and read_path on the model class:
class Upload < ActiveRecord::Base acts_as_file self.save_path = File.join(RAILS_ROOT, 'public', 'uploads') self.read_path = 'uploads' end
Further Documentation
There is currently no advanced documentation for this plugin.
New documentationEdit plugin | Back in time (1 older version) | Last edited by: scott, 8 months ago

