Created
March 16, 2011 20:47
-
-
Save hidenowt/873268 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
require 'rubygems' | |
require 'sinatra' | |
require 'dm-core' | |
require 'dm-validations' | |
require 'dm-timestamps' | |
# bem na apostila esqueceu de incluir essa gem | |
require 'dm-migrations' | |
# lembrar de informar na apostila a instalação da gem dm-mysql-adapter | |
DataMapper.setup(:default, "mysql://localhost/textos") | |
class Texto | |
include DataMapper::Resource | |
# correção aqui, antes era: | |
# property :id, Integer, :serial => true | |
# agora é: agora ele tem o tipo Serial em vez de usar o tipo Integer passando parametros | |
property :id, Serial | |
property :conteudo, Text, :required => false | |
property :created_at, DateTime | |
property :updated_at, DateTime | |
# correção aqui, antes era: | |
# validates_length :conteudo, :minimum => 3 | |
# agora é: com o _of no validates | |
validates_length_of :conteudo, :minimum => 3 | |
end | |
# esse comando agora só funciona se tiver a gem dm-migrations | |
DataMapper.auto_upgrade! | |
# new | |
get '/' do | |
erb :new | |
end | |
# create | |
post '/' do | |
@texto = Texto.new(:conteudo => params[:texto_conteudo]) | |
if @texto.save | |
redirect "/#{@texto.id}" | |
else | |
redirect '/' | |
end | |
end | |
# show | |
get '/:id' do | |
@texto = Texto.get(params[:id]) | |
if @texto | |
erb :show | |
else | |
redirect '/' | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment