Skip to content

Instantly share code, notes, and snippets.

@mamantoha
Created May 22, 2012 20:12
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mamantoha/2771347 to your computer and use it in GitHub Desktop.
Save mamantoha/2771347 to your computer and use it in GitHub Desktop.
CarrierWave and DataMapper
# encoding: utf-8
require 'sinatra'
require 'slim'
require 'data_mapper'
require 'carrierwave'
require 'carrierwave/datamapper'
class ImageUploader < CarrierWave::Uploader::Base
include CarrierWave::RMagick
storage :file
process :resize_to_fit => [800, 800]
version :thumb do
process :resize_to_fill => [200, 200]
end
def store_dir
'uploads' # public/uploads
end
def extension_white_list
%w(bmp gif jpg jpeg png)
end
end
# Налаштування підключення до бази даних.
DataMapper.setup(:default, "sqlite3://#{Dir.pwd}/photogallery.sqlite3")
# Таблиця для зберігання інформації про зображення у галереї
class Image
include DataMapper::Resource
property :id, Serial
property :title, Text
mount_uploader :image, ImageUploader
end
DataMapper.auto_upgrade!
# Список зображень
get '/' do
@images = Image.all
slim :index
end
# Створення нового зображення
post '/' do
image = Image.new(:title => params[:title])
image.image = params[:image]
image.save
redirect '/'
end
__END__
@@ index
- @images.each do |image|
p
= image.title
a href="#{image.image.url}"
img src="#{image.image.thumb.url}"
form action='/' method='post' enctype='multipart/form-data'
fieldset
p
label for='title' Title:
input name='title' type='text'
p
label for='image' Image:
input name='image' type='file'
p
input type='submit' value='Save'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment