Project

attachable

0.0
No commit activity in last 3 years
No release in over 3 years
Add methods to automatically manage a file as part of a Rails model
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
 Dependencies
 Project Readme
Attachable
==========

A really simple file attachment plugin for Rails 3.  Designed to work with any generic files, i.e not specific to images.
It will not do any magical processing for you, it just stores the data in the database.  Write your own magic.


Usage
=======

Add the following fields to your table:

add_column :medias, :file_name, :string
add_column :medias, :file_type, :string
add_column :medias, :file_size, :integer
add_column :medias, :file_data, :binary, :limit => 2.megabytes

In your model:
class Media < ActiveRecord::Base  
  attachable
end

In a controller that should be able to view the result:
class MediasController < ApplicationController
  def show
    @media = Media(params[:id])
    send_data @media.file_contents, :filename => @media.file_name, :type => @media.file_type, :disposition => 'inline'
  end
end

To upload, just set the form field to :file AND remember the multipart => true bit like shown:
<% form_for(@media, :html => { :multipart => true }) do |f| %>
  <%= f.file_field :file %>
  ....more stuff....


In theory you can change the prefix from "file" to anything by setting the file_prefix option in the model, but I haven't tested it.

Copyright (c) 2010 Brian Michalski, released under the MIT license