Skip to content

Instantly share code, notes, and snippets.

@macek
Created October 4, 2010 22:38
Show Gist options
  • Save macek/610596 to your computer and use it in GitHub Desktop.
Save macek/610596 to your computer and use it in GitHub Desktop.
How to save uploaded files to your database in Rails
# db/migrate/20101004063749_create_photos.rb
class CreatePhotos < ActiveRecord::Migration
def self.up
create_table :photos do |t|
t.string :name, :null => false
t.binary :data, :null => false
t.string :filename
t.string :mime_type
t.timestamps
end
end
def self.down
drop_table :photos
end
end
# app/views/photos/new.html.erb
<%= form_for(@photo, :html => {:multipart => true}) do |f| %>
<div class="field">
<%= f.label :name %>
<%= f.text_field :name %>
</div>
<div class="field">
<%= f.label :data %>
<%= f.file_field :data %>
</div>
<div class="actions">
<%= f.submit "Upload" %>
</div>
<% end %>
# app/controllers/photos_controller.rb
class PhotosController < ApplicationController
def index
@photos = Photo.all
end
def show
@photo = Photo.find(params[:id])
end
def new
@photo = Photo.new
end
def create
# build a photo and pass it into a block to set other attributes
@photo = Photo.new(params[:photo]) do |t|
if params[:photo][:data]
t.data = params[:photo][:data].read
t.filename = params[:photo][:data].original_filename
t.mime_type = params[:photo][:data].content_type
end
end
# normal save
if @photo.save
redirect_to(@photo, :notice => 'Photo was successfully created.')
else
render :action => "new"
end
end
def destroy
@photo = Photo.find(params[:id])
@photo.destroy
redirect_to(photos_url)
end
end
# app/controllers/photos_controller.rb
class PhotosController < ApplicationController
# ...
def serve
@photo = Photo.find(params[:id])
send_data(@photo.data, :type => @photo.mime_type, :filename => "#{@photo.name}.jpg", :disposition => "inline")
end
end
# rails3
resources :photos do
get "serve", :on => :member
end
# rails 2.x
resources :photos, :member => {:serve => :get}
<%= image_tag serve_photo_path(@photo) %>
@lecabel
Copy link

lecabel commented May 9, 2015

Great tutorial, thanks. I'm having an issue when the user wants to edit the file uploaded. I tried like below in update:
def update
if params[:record_revision][:filename]
@record_revision = RecordRevision.find(params[:id]) do |t|
t.file_contents = params[:record_revision][:filename].read
t.filename = params[:record_revision][:filename].original_filename
t.content_type = params[:record_revision][:filename].content_type
end
end
but still have "... undefined method `name' for nil:NilClass ..."
could you help on that?
thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment