Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
How to use 'Mongoid' gem

ADE

$ ruby -v
ruby 2.4.2p198 (2017-09-14 revision 59899) [x86_64-linux]

$ bundle exec rails -v
Rails 5.1.4

Gemfile

gem 'mongoid', '~> 6.1.0

Create Mongoid config

$ bundle exec rails g mongoid:config

Detail of Mongoid config

$ cat config/mongoid.yml

development:
  # Configure available database clients. (required)
  clients:
    # Defines the default client. (required)
    default:
      # Defines the name of the default database that Mongoid can connect to.
      # (required).
      database: vuejs_test_development
      # Provides the hosts the default client can connect to. Must be an array
      # of host:port pairs. (required)
      hosts:
        - localhost:27017
      options:
        # Change the default write concern. (default = { w: 1 })
        # write:
        #   w: 1
        :

Create Scaffold

$ bundle exec rails g scaffold Dog name:string age:integer
      invoke  mongoid
      create    app/models/dog.rb
      invoke    test_unit
      create      test/models/dog_test.rb
      create      test/fixtures/dogs.yml
      invoke  resource_route
       route    resources :dogs
      invoke  scaffold_controller
      create    app/controllers/dogs_controller.rb
      invoke    erb
      create      app/views/dogs
      create      app/views/dogs/index.html.erb
      create      app/views/dogs/edit.html.erb
      create      app/views/dogs/show.html.erb
      create      app/views/dogs/new.html.erb
      create      app/views/dogs/_form.html.erb
      invoke    test_unit
      create      test/controllers/dogs_controller_test.rb
      invoke    helper
      create      app/helpers/dogs_helper.rb
      invoke      test_unit
      invoke    jbuilder
      create      app/views/dogs/index.json.jbuilder
      create      app/views/dogs/show.json.jbuilder
      create      app/views/dogs/_dog.json.jbuilder
      invoke  test_unit
      create    test/system/dogs_test.rb
      invoke  assets
      invoke    coffee
      create      app/assets/javascripts/dogs.coffee
      invoke    scss
      create      app/assets/stylesheets/dogs.scss
      invoke  scss
      create    app/assets/stylesheets/scaffolds.scss

Controller

class DogsController < ApplicationController
  :
  :
  def create
    @dog = Dog.new(dog_params)

    respond_to do |format|
      if @dog.save
        format.html { redirect_to @dog, notice: 'Dog was successfully created.' }
        format.json { render :show, status: :created, location: @dog }
      else
        format.html { render :new }
        format.json { render json: @dog.errors, status: :unprocessable_entity }
      end
    end
  end

  private

    def dog_params
      params.require(:dog).permit(:name, :age)
    end
end 

View

<%= form_with(model: dog, local: true) do |form| %>
  <% if dog.errors.any? %>
    <div id="error_explanation">
      <h2><%= pluralize(dog.errors.count, "error") %> prohibited this dog from being saved:</h2>

      <ul>
      <% dog.errors.full_messages.each do |message| %>
        <li><%= message %></li>
      <% end %>
      </ul>
    </div>
  <% end %>

  <div class="field">
    <%= form.label :name %>
    <%= form.text_field :name, id: :dog_name %>
  </div>

  <div class="field">
    <%= form.label :age %>
    <%= form.number_field :age, id: :dog_age %>
  </div>

  <div class="actions">
    <%= form.submit %>
  </div>
<% end %>

Model

class Dog # without ActiveRecord Class
  include Mongoid::Document
  field :name, type: String
  field :age, type: Integer
end

Create data

Post logs

$ bundle exec rails s -b 0.0.0.0
=> Booting Puma
=> Rails 5.1.4 application starting in development 
=> Run `rails server -h` for more startup options
Puma starting in single mode...
* Version 3.11.3 (ruby 2.4.2-p198), codename: Love Song
* Min threads: 5, max threads: 5
* Environment: development
* Listening on tcp://0.0.0.0:3000
Use Ctrl-C to stop

Started POST "/dogs" for 10.0.2.2 at 2018-04-07 04:10:09 +0100
Cannot render console from 10.0.2.2! Allowed networks: 127.0.0.1, ::1, 127.0.0.0/127.255.255.255
Processing by DogsController#create as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"UDXZ7CwTur6z0cHlZQyJ0kzIfT9pp2EnX6HR3T+mcEuqK8EygbAP6ULDTIw6yL6HxSmJ0IYGtfR87ABq4XWDLQ==", "dog"=>{"name"=>"Taro", "age"=>"12"}, "commit"=>"Create Dog"}
MONGODB | localhost:27017 | vuejs_test_development.insert | STARTED | {"insert"=>"dogs", "ordered"=>true, "lsid"=>{"id"=><BSON::Binary:0x70154264749240 type=uuid data=0x087d8e0edb794398...>}, "documents"=>[{"_id"=>BSON::ObjectId('5ac83691e138236b2df1d034'), "name"=>"Taro", "age"=>12}]}
MONGODB | localhost:27017 | vuejs_test_development.insert | SUCCEEDED | 0.034509744s
Redirected to http://localhost:3001/dogs/5ac83691e138236b2df1d034
Completed 302 Found in 41ms


Started GET "/dogs/5ac83691e138236b2df1d034" for 10.0.2.2 at 2018-04-07 04:10:09 +0100
Cannot render console from 10.0.2.2! Allowed networks: 127.0.0.1, ::1, 127.0.0.0/127.255.255.255
Processing by DogsController#show as HTML
  Parameters: {"id"=>"5ac83691e138236b2df1d034"}
MONGODB | localhost:27017 | vuejs_test_development.find | STARTED | {"find"=>"dogs", "filter"=>{"_id"=>BSON::ObjectId('5ac83691e138236b2df1d034')}, "lsid"=>{"id"=><BSON::Binary:0x70154264749240 type=uuid data=0x087d8e0edb794398...>}}
MONGODB | localhost:27017 | vuejs_test_development.find | SUCCEEDED | 0.0008173589999999999s
  Rendering dogs/show.html.erb within layouts/application
  Rendered dogs/show.html.erb within layouts/application (1.0ms)
Completed 200 OK in 59ms (Views: 41.2ms)

Special thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment