Created
May 22, 2012 20:12
-
-
Save mamantoha/2771347 to your computer and use it in GitHub Desktop.
CarrierWave and DataMapper
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
# 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