Forked from macek/20101004063749_create_photos.rb
Last active
August 29, 2015 14:25
-
-
Save alekpopovic/ab3a831b802cd88ba5cc to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 %> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# rails3 | |
resources :photos do | |
get "serve", :on => :member | |
end | |
# rails 2.x | |
resources :photos, :member => {:serve => :get} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<%= image_tag serve_photo_path(@photo) %> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment