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) %>
@shruti28
Copy link

how to send these images as mail attachment?plz help..

@ssandeep
Copy link

It depends on how you are sending the email. Whether you're using action mailer or some other gem, there is a provision to specify the attachments. You can use the same send_data mentioned above in the "serve" action.

@LeoJavaAI
Copy link

Will this work with rails 4?

@Senjai
Copy link

Senjai commented Dec 2, 2013

@leo-g Not as written. The only thing you'd have to add is a function (if you want) or the like to sanitize the parameters according to rails 4 strong params.

Read more here: http://edgeguides.rubyonrails.org/action_controller_overview.html#strong-parameters

@papapoison
Copy link

Does this have a corresponding model? If not, what's the benefit of omitting that?

@rcp140875
Copy link

I got an error:

ActiveModel::ForbiddenAttributesError in PhotosController#create
:(
I am new in Rails

@dominathan
Copy link

as Senjai was saying, you have to sanitize the data for rails 4 strong params, e.g., in your controller,

private
def photo_params
params(:photo).permit(:parameter_name_1,:parameter_name2)
end

then call photo_params in your new actions

@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