Skip to content

Instantly share code, notes, and snippets.

@brntbeer
Last active July 22, 2019 21:17
Show Gist options
  • Save brntbeer/201246 to your computer and use it in GitHub Desktop.
Save brntbeer/201246 to your computer and use it in GitHub Desktop.
lol a super old sinatra app that was going to be for pictures of really bad conference or hotel rugs
require 'rubygems'
require 'sinatra'
require 'dm-core'
require 'haml'
require 'lib/models'
#just covering my bases!
['/', '/index/?', '/home/?'].each do |path|
get path do
redirect '/rugs/'
end
end
get '/rugs/?' do
@image = Images.all
haml :home
end
#grabs the picture from the file upload location and stores it
#with paperclip which is rendered from the haml view.
post '/upload' do
unless params[:image] &&
(@tmpfile = params[:image][:tempfile]) &&
(@name = params[:image][:filename])
@error = "No file selected"
return haml(:error)
end
STDERR.puts "Uploading file, original name #{@name.inspect}"
while blk = @tmpfile.read(65536)
# here you would write it to its final location
Images.create(:image => blk, :created_at => Time.now.utc, :text => blk.inspect)
end
"Upload complete"
end
get '/upload' do
haml(:upload)
end
---------------------------------- models.rb
require 'dm-paperclip'
DataMapper::setup(:default, ENV['DATABASE_URL'] || 'sqlite3://ugly_rugs.db')
class Images
include DataMapper::Resource
include DataMapper::Validate
include Paperclip::Resource
property :id, Serial
property :created_at, DateTime
property :text, String
has_attached_file :image,
:deafult_url => "/tmp/missing_file.jpg",
:url => "/tmp/:image.:extension",
:path => "/tmp/:image.:extension"
validates_attachment_presence :image
end
DataMapper.auto_migrate!
------------------------------home.haml
-#images need to have a loop to show all existing ones
!!! strict
%h1.header HI!
%table
- @image.each do |i|
%tr
%td
%img{:src => "#{i.image}"}
=haml(:navigation, :layout => false)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment