Add the following gems to your Gem file of your rails app
gem 'mongoid', github: 'mongoid/mongoid'
gem 'bson'
# gem 'bson_ext', '~> 1.9.2'
make bundle install
rails g mongoid:config
First in config/application.rb comment out the line and add the following to enable mongodb
# require 'rails/all'
require "action_controller/railtie"
require "action_mailer/railtie"
# require "active_resource/railtie"
require "sprockets/railtie"
In config/environments/development.rb, comment out this line
# config.active_record.migration_error = :page_load
Let’s generate our Post first, it will have 3 fields: a title, a body and it can be starred.
rails g scaffold post title:string body:string starred:boolean
Add fields to your model
class Post
include Mongoid::Document
include Mongoid::Timestamps
field :title, type: String
field :body, type: String
field :starred, type: Boolean
index({ starred: 1 })
end
Now we have a command to enforce indexes on the database
rake db:mongoid:create_indexes
if you've the database connection error, try doing the following two things
uncomment 127.0.0.1 host line in C:\Windows\System32\drivers\etc\hosts
or
in mongoid.yml file, change the hosts to the following
hosts:
- 127.0.0.1:27017
Read more at
http://requiremind.com/riding-rails-4-along-with-mongoid-and-ruby-2-dot-0/
gem 'mongoid', github: 'mongoid/mongoid'
gem 'bson'
# gem 'bson_ext', '~> 1.9.2'
make bundle install
rails g mongoid:config
First in config/application.rb comment out the line and add the following to enable mongodb
# require 'rails/all'
require "action_controller/railtie"
require "action_mailer/railtie"
# require "active_resource/railtie"
require "sprockets/railtie"
In config/environments/development.rb, comment out this line
# config.active_record.migration_error = :page_load
Let’s generate our Post first, it will have 3 fields: a title, a body and it can be starred.
rails g scaffold post title:string body:string starred:boolean
Add fields to your model
class Post
include Mongoid::Document
include Mongoid::Timestamps
field :title, type: String
field :body, type: String
field :starred, type: Boolean
index({ starred: 1 })
end
Now we have a command to enforce indexes on the database
rake db:mongoid:create_indexes
if you've the database connection error, try doing the following two things
uncomment 127.0.0.1 host line in C:\Windows\System32\drivers\etc\hosts
or
in mongoid.yml file, change the hosts to the following
hosts:
- 127.0.0.1:27017
Read more at
http://requiremind.com/riding-rails-4-along-with-mongoid-and-ruby-2-dot-0/
Adding devise on Mongoid
Configure ActionMailer
Set up action_mailer in your development environment in the file
config/environments/development.rb
by commenting out the line in the file:
# Don't care if the mailer can't send
# config.action_mailer.raise_delivery_errors = false
and adding:
# ActionMailer Config
config.action_mailer.default_url_options = { :host => 'localhost:3000' }
# A dummy setup for development - no deliveries, but logged
config.action_mailer.delivery_method = :smtp
config.action_mailer.perform_deliveries = false
config.action_mailer.raise_delivery_errors = true
config.action_mailer.default :charset => "utf-8"
Set up action_mailer in your production environment in the file
config/environments/production.rb
by adding:
config.action_mailer.default_url_options = { :host => 'yourhost.com' }
# ActionMailer Config
# Setup for production - deliveries, no errors raised
config.action_mailer.delivery_method = :smtp
config.action_mailer.perform_deliveries = true
config.action_mailer.raise_delivery_errors = false
config.action_mailer.default :charset => "utf-8"
Installing devise
$ rails generate devise:install
which installs a configuration file:
config/initializers/devise.rb
and a localization file.
Devise will recognize that you already have Mongoid installed and it will set its ORM configuration in the config/initializers/devise.rb file to include:
require 'devise/orm/mongoid'
Adding devise users
Generate a Model and Routes for Users
Devise can manage users and administrators separately, allowing two (or more) roles to be implemented differently. For this example, we just implement Users.
Use Devise to generate models and routes for a User:
$ rails generate devise User
Devise will recognize that Mongoid is installed and set up the User model with
include Mongoid::Document
which must precede all other statements in the model.
Devise will modify the config/routes.rb file to add:
devise_for :users
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
Add these manually in developer.rb model class to generate routes
# traditional devise need to manually place in mongodb generated devisedevise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
Modify the file models/user.rb and add:
field :name
validates_presence_of :name
validates_uniqueness_of :name, :email, :case_sensitive => false
attr_accessible :name, :email, :password, :password_confirmation, :remember_me
rails g devise:views to add view files
# config.active_record.dump_schema_after_migration = false
# Configure available database sessions. (required)
sessions:
# Defines the default session. (required)
default:
database: analytics_server_rails_production
# Provides the hosts the default session can connect to. Must be an array
# of host:port pairs. (required)
hosts:
- 127.0.0.1:27017
options:
# Change the default write concern. (default = { w: 1 })
# write:
# w: 1
# Change the default consistency model to primary, secondary.
# 'secondary' will send reads to secondaries, 'primary' sends everything
# to master. (default: primary)
# read: secondary_preferred
# How many times Moped should attempt to retry an operation after
# failure. (default: The number of nodes in the cluster)
# max_retries: 20
# The time in seconds that Moped should wait before retrying an
# operation on failure. (default: 0.25)
# retry_interval: 0.25
# Configure Mongoid specific options. (optional)
options:
Set Up a Demonstration of Devise
$ rails generate controller users show
Note that “users” is plural when you create the controller.
Modify the file app/controllers/users_controller.rb and add:
before_filter :authenticate_user!
def show
@user = User.find(params[:id])
end
The file config/routes.rb has already been modified to include:
get "users/show"
Remove that and change the routes to:
root :to => "home#index"
devise_for :users
resources :users
Important note: The devise_for :users route must be placed above resources :users.
This line needs to be commented out in config/nitialiser/cookies_serialiser.rb or you will get cookies errors while logging in
# Rails.application.config.action_dispatch.cookies_serializer = :jsonComment out the following line to stop conflict with previous active records
# Do not dump schema after migrations.# config.active_record.dump_schema_after_migration = false
Add production config in config/mongoid.yml file
production:# Configure available database sessions. (required)
sessions:
# Defines the default session. (required)
default:
database: analytics_server_rails_production
# Provides the hosts the default session can connect to. Must be an array
# of host:port pairs. (required)
hosts:
- 127.0.0.1:27017
options:
# Change the default write concern. (default = { w: 1 })
# write:
# w: 1
# Change the default consistency model to primary, secondary.
# 'secondary' will send reads to secondaries, 'primary' sends everything
# to master. (default: primary)
# read: secondary_preferred
# How many times Moped should attempt to retry an operation after
# failure. (default: The number of nodes in the cluster)
# max_retries: 20
# The time in seconds that Moped should wait before retrying an
# operation on failure. (default: 0.25)
# retry_interval: 0.25
# Configure Mongoid specific options. (optional)
options:
No comments:
Post a Comment