Hello MongoDB
Posted by Ryan Baxter Thu, 18 Feb 2010 04:11:00 GMT
In an effort to jump-start my creativity, I thought I’d hop on the NoSQL train and work up a little “Hello World” app using MongoDB with Sinatra and MongoMapper. In a later project I’ll be throwing Haml into the mix and working completely outside of my comfort zone. Dependencies be damned!
require 'rubygems'
require 'mongo_mapper'
require 'sinatra'
MongoMapper.connection = Mongo::Connection.new('localhost')
MongoMapper.database = 'messages'
class Message
include MongoMapper::Document
key :message, String
key :ip_address, String
timestamps!
end
get '/' do
Message.all.map { |m|
time = m.created_at.localtime
"On #{ time.strftime('%m/%d/%Y') } at " \
"#{ time.strftime('%I:%M %p')} <strong>#{ m.ip_address }</strong> said, " \
"<strong>\"#{ m.message }\"</strong>"
}.join "<br />"
end
get '/:message' do
Message.create(:message => params[:message], :ip_address => @env['REMOTE_ADDR']).save
redirect '/'
endOn 02/17/2010 at 10:13 PM 127.0.0.1 said, “Hello World!”
- Posted in Code Snippets
- Meta no comments, permalink, rss, atom

