Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
9 changes: 4 additions & 5 deletions source/quick-start-sinatra.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ Quick Start (Sinatra)
:values: tutorial

.. meta::
:keywords: php framework, odm
:keywords: ruby framework, odm

.. contents:: On this page
:local:
Expand Down Expand Up @@ -58,7 +58,6 @@ that connects to a MongoDB deployment.
/quick-start-sinatra/create-a-deployment/
/quick-start-sinatra/create-a-connection-string/
/quick-start-sinatra/configure-mongodb/

.. /quick-start-sinatra/view-data/
.. /quick-start-sinatra/write-data/
.. /quick-start-sinatra/next-steps/
/quick-start-sinatra/view-data/
/quick-start-sinatra/write-data/
/quick-start-sinatra/next-steps/
31 changes: 31 additions & 0 deletions source/quick-start-sinatra/next-steps.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
.. _mongoid-quick-start-sinatra-next-steps:

==========
Next Steps
==========

.. facet::
:name: genre
:values: reference

.. meta::
:keywords: learn more

Congratulations on completing the Quick Start tutorial for Sinatra!

After you complete these steps, you have a Sinatra web application that
uses {+odm+} to connect to your MongoDB deployment, run a query on
the sample data, and render retrieved results.

.. TODO You can download the completed web application project by cloning the
.. `mongoid-quickstart <>`__
.. GitHub repository.

.. TODO Learn more about {+odm+} features from the following resources:

.. - :ref:`mongoid-fundamentals-connection`: learn how to configure your MongoDB
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

S: capitalize the first word after these colons

Suggested change
.. - :ref:`mongoid-fundamentals-connection`: learn how to configure your MongoDB
.. - :ref:`mongoid-fundamentals-connection`: Learn how to configure your MongoDB

.. connection.
..
.. - :ref:`mongoid-usage-examples`: see code examples of frequently used MongoDB
.. operations.

150 changes: 150 additions & 0 deletions source/quick-start-sinatra/view-data.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
.. _mongoid-quick-start-sinatra-view-data:

=================
View MongoDB Data
=================

.. facet::
:name: genre
:values: tutorial

.. meta::
:keywords: test connection, runnable, code example

.. procedure::
:style: connected

.. step:: Create an application file

At the root level of your project, create a file called ``app.rb``.
Paste the following contents into the ``app.rb`` file to
load the necessary gems and your configuration file:

.. code-block:: ruby

require 'sinatra'
require 'mongoid'

Mongoid.load!(File.join(File.dirname(__FILE__), 'config', 'mongoid.yml'))

.. step:: Create a data model

In the ``app.rb`` file, create a model called ``Restaurant``
to represent data from the sample ``restaurants`` collection in
your MongoDB database:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

S: specify the database

Suggested change
your MongoDB database:
the ``sample_restaurants`` database:


.. code-block:: ruby

class Restaurant
include Mongoid::Document

field :name, type: String
field :cuisine, type: String
field :borough, type: String

end

.. step:: Generate a view

To display your data in a specified way by using HTML, you can
create a **view**.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

S: since this is a procedure step, I'd change the verb tense:

Suggested change
To display your data in a specified way by using HTML, you can
create a **view**.
Create a **view** to display your data in a specified way by using HTML.


At the root level of your project, create a directory
called ``views``. Then, create a file called
``list_restaurants.erb``. Paste the following code into the
``list_restaurants.erb`` file:

.. code-block:: html

<!DOCTYPE html>
<html>
<head>
<title>Restaurants List</title>
</head>
<body>
<h1>Restaurants List</h1>
<table border="1">
<tr>
<th>Name</th>
<th>Cuisine</th>
<th>Borough</th>
</tr>
<% @restaurants.each do |restaurant| %>
<tr>
<td><%= restaurant.name %></td>
<td><%= restaurant.cuisine %></td>
<td><%= restaurant.borough %></td>
</tr>
<% end %>
</table>
</body>
</html>

.. step:: Add a web route

In the ``app.rb`` file, add a ``get`` route called
``list_restaurants``, as shown in the following code:

.. code-block:: ruby

get '/list_restaurants' do
@restaurants = Restaurant
.where(name: /earth/i)

erb :list_restaurants
end

This route retrieves restaurant documents in which the value of
the ``name`` field contains the string ``"earth"``. The route
uses the ``list_restaurants`` view to render the results.

.. _mongoid-quick-start-sinatra-json:

.. step:: (Optional) View your results as JSON documents

Instead of generating a view to render your results, you can
use the ``to_json()`` method to display your results in JSON
format.

Replace the ``list_restaurants`` route with the following code to
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

S: clarify which file you're editing

Suggested change
Replace the ``list_restaurants`` route with the following code to
Replace the ``list_restaurants`` route in the ``app.rb`` file with the following code to

retrieve results and return them as JSON documents:

.. code-block:: ruby

get '/list_restaurants' do
restaurants = Restaurant
.where(name: /earth/i)

restaurants.to_json
end

.. step:: Start your Sinatra application

Run the following command from the application root directory
to start your {+language+} web server:

.. code-block:: bash

bundle exec ruby app.rb

After the server starts, it outputs the following message
indicating that the application is running on port ``4567``:

.. code-block:: none
:copyable: false

[2024-10-01 12:36:49] INFO WEBrick 1.8.2
[2024-10-01 12:36:49] INFO ruby 3.2.5 (2024-07-26) [arm64-darwin23]
== Sinatra (v4.0.0) has taken the stage on 4567 for development with backup from WEBrick
[2024-10-01 12:36:49] INFO WEBrick::HTTPServer#start: pid=79176 port=4567

.. step:: View the restaurant data

Open the URL http://localhost:4567/list_restaurants in your web browser.
The page shows a list of restaurants and details about each of
them:

.. figure:: /includes/figures/quickstart-sinatra-list.png
:alt: The rendered list of restaurants

.. include:: /includes/quick-start/troubleshoot.rst
43 changes: 43 additions & 0 deletions source/quick-start-sinatra/write-data.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
.. _mongoid-quick-start-sinatra-write-data:

=====================
Write Data to MongoDB
=====================

.. facet::
:name: genre
:values: tutorial

.. meta::
:keywords: test connection, runnable, code example

.. procedure::
:style: connected

.. step:: Add a web route to insert data

In the ``app.rb`` file, add a ``post`` route called
``add_restaurant``, as shown in the following code:

.. code-block:: ruby

post '/add_restaurant' do
Restaurant.create!(params[:restaurant])
end

.. step:: Post a request to create a restaurant entry

Send a ``Restaurant`` instance to the ``add_restaurant`` endpoint
by running the following command in your shell:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

S: Clarify where you should run the command

Suggested change
by running the following command in your shell:
from the application root directory:


.. code-block:: bash

curl -d 'restaurant[name]=Good+Earth+Cafe&restaurant[cuisine]=Cafe&restaurant[borough]=Queens' http://localhost:4567/add_restaurant

.. step:: View the data

Refresh http://localhost:4567/list_restaurants in your web browser
to view the new restaurant entry that you submitted. The inserted
restaurant appears at the bottom of the list.

.. include:: /includes/quick-start/troubleshoot.rst
Loading