For the past couple of hours I am trying to insert a simple document
into MongoDb database using Rails and MongoMapper. There is no error
but the document never gets inserted. Here is my simple class:
class Post
include MongoMapper::Document
key :title, String, :required => true
key :description, String
end
My database.yml file:
development:
adapter: mongodb
host: localhost
database: blog
test: &TEST
adapter: mongodb
host: localhost
database: blog_test
production:
adapter: mongodb
host: localhost
database: blog
and my mongodb.rb file in initializer:
db_config = YAML::load(File.read(RAILS_ROOT + “/config/database.yml”))
if db_config[Rails.env] && db_config[Rails.env][‘adapter’] ==
‘mongodb’
mongo = db_config[‘blog’]
MongoMapper.connection = Mongo::Connection.new(mongo[‘hostname’])
MongoMapper.database = mongo[‘database’]
end
And finally here is the controller create action:
def create
post = Post.new
post.title = 'title'
post.save
redirect_to :action => "new"
end