Skip to content

Commit a1a3bb3

Browse files
authored
Merge pull request #50 from rustagir/DOCSP-44008-read-write-sinatra
DOCSP-44008: read/write sinatra quickstart
2 parents 3f8e885 + b7de8b0 commit a1a3bb3

File tree

5 files changed

+231
-5
lines changed

5 files changed

+231
-5
lines changed
202 KB
Loading

source/quick-start-sinatra.txt

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ Quick Start (Sinatra)
99
:values: tutorial
1010

1111
.. meta::
12-
:keywords: php framework, odm
12+
:keywords: ruby framework, odm
1313

1414
.. contents:: On this page
1515
:local:
@@ -58,7 +58,6 @@ that connects to a MongoDB deployment.
5858
/quick-start-sinatra/create-a-deployment/
5959
/quick-start-sinatra/create-a-connection-string/
6060
/quick-start-sinatra/configure-mongodb/
61-
62-
.. /quick-start-sinatra/view-data/
63-
.. /quick-start-sinatra/write-data/
64-
.. /quick-start-sinatra/next-steps/
61+
/quick-start-sinatra/view-data/
62+
/quick-start-sinatra/write-data/
63+
/quick-start-sinatra/next-steps/
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
.. _mongoid-quick-start-sinatra-next-steps:
2+
3+
==========
4+
Next Steps
5+
==========
6+
7+
.. facet::
8+
:name: genre
9+
:values: reference
10+
11+
.. meta::
12+
:keywords: learn more
13+
14+
Congratulations on completing the Quick Start tutorial for Sinatra!
15+
16+
After you complete these steps, you have a Sinatra web application that
17+
uses {+odm+} to connect to your MongoDB deployment, run a query on
18+
the sample data, and render retrieved results.
19+
20+
.. TODO You can download the completed web application project by cloning the
21+
.. `mongoid-quickstart <>`__
22+
.. GitHub repository.
23+
24+
.. TODO Learn more about {+odm+} features from the following resources:
25+
26+
.. - :ref:`mongoid-fundamentals-connection`: Learn how to configure your MongoDB
27+
.. connection.
28+
..
29+
.. - :ref:`mongoid-usage-examples`: See code examples of frequently used MongoDB
30+
.. operations.
31+
Lines changed: 150 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,150 @@
1+
.. _mongoid-quick-start-sinatra-view-data:
2+
3+
=================
4+
View MongoDB Data
5+
=================
6+
7+
.. facet::
8+
:name: genre
9+
:values: tutorial
10+
11+
.. meta::
12+
:keywords: test connection, runnable, code example
13+
14+
.. procedure::
15+
:style: connected
16+
17+
.. step:: Create an application file
18+
19+
At the root level of your project, create a file called ``app.rb``.
20+
Paste the following contents into the ``app.rb`` file to
21+
load the necessary gems and your configuration file:
22+
23+
.. code-block:: ruby
24+
25+
require 'sinatra'
26+
require 'mongoid'
27+
28+
Mongoid.load!(File.join(File.dirname(__FILE__), 'config', 'mongoid.yml'))
29+
30+
.. step:: Create a data model
31+
32+
In the ``app.rb`` file, create a model called ``Restaurant``
33+
to represent data from the sample ``restaurants`` collection in
34+
the ``sample_restaurants`` database:
35+
36+
.. code-block:: ruby
37+
38+
class Restaurant
39+
include Mongoid::Document
40+
41+
field :name, type: String
42+
field :cuisine, type: String
43+
field :borough, type: String
44+
45+
end
46+
47+
.. step:: Generate a view
48+
49+
Create a **view** to display your data in a specified way by using
50+
HTML and {+language+}.
51+
52+
At the root level of your project, create a directory
53+
called ``views``. Then, create a file called
54+
``list_restaurants.erb``. Paste the following code into the
55+
``list_restaurants.erb`` file:
56+
57+
.. code-block:: html
58+
59+
<!DOCTYPE html>
60+
<html>
61+
<head>
62+
<title>Restaurants List</title>
63+
</head>
64+
<body>
65+
<h1>Restaurants List</h1>
66+
<table border="1">
67+
<tr>
68+
<th>Name</th>
69+
<th>Cuisine</th>
70+
<th>Borough</th>
71+
</tr>
72+
<% @restaurants.each do |restaurant| %>
73+
<tr>
74+
<td><%= restaurant.name %></td>
75+
<td><%= restaurant.cuisine %></td>
76+
<td><%= restaurant.borough %></td>
77+
</tr>
78+
<% end %>
79+
</table>
80+
</body>
81+
</html>
82+
83+
.. step:: Add a web route
84+
85+
In the ``app.rb`` file, add a ``get`` route called
86+
``list_restaurants``, as shown in the following code:
87+
88+
.. code-block:: ruby
89+
90+
get '/list_restaurants' do
91+
@restaurants = Restaurant
92+
.where(name: /earth/i)
93+
94+
erb :list_restaurants
95+
end
96+
97+
This route retrieves restaurant documents in which the value of
98+
the ``name`` field contains the string ``"earth"``. The route
99+
uses the ``list_restaurants`` view to render the results.
100+
101+
.. _mongoid-quick-start-sinatra-json:
102+
103+
.. step:: (Optional) View your results as JSON documents
104+
105+
Instead of generating a view to render your results, you can
106+
use the ``to_json()`` method to display your results in JSON
107+
format.
108+
109+
Replace the ``list_restaurants`` route in the ``app.rb`` file with
110+
the following code to return the results as JSON documents:
111+
112+
.. code-block:: ruby
113+
114+
get '/list_restaurants' do
115+
restaurants = Restaurant
116+
.where(name: /earth/i)
117+
118+
restaurants.to_json
119+
end
120+
121+
.. step:: Start your Sinatra application
122+
123+
Run the following command from the application root directory
124+
to start your {+language+} web server:
125+
126+
.. code-block:: bash
127+
128+
bundle exec ruby app.rb
129+
130+
After the server starts, it outputs the following message
131+
indicating that the application is running on port ``4567``:
132+
133+
.. code-block:: none
134+
:copyable: false
135+
136+
[2024-10-01 12:36:49] INFO WEBrick 1.8.2
137+
[2024-10-01 12:36:49] INFO ruby 3.2.5 (2024-07-26) [arm64-darwin23]
138+
== Sinatra (v4.0.0) has taken the stage on 4567 for development with backup from WEBrick
139+
[2024-10-01 12:36:49] INFO WEBrick::HTTPServer#start: pid=79176 port=4567
140+
141+
.. step:: View the restaurant data
142+
143+
Open the URL http://localhost:4567/list_restaurants in your web browser.
144+
The page shows a list of restaurants and details about each of
145+
them:
146+
147+
.. figure:: /includes/figures/quickstart-sinatra-list.png
148+
:alt: The rendered list of restaurants
149+
150+
.. include:: /includes/quick-start/troubleshoot.rst
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
.. _mongoid-quick-start-sinatra-write-data:
2+
3+
=====================
4+
Write Data to MongoDB
5+
=====================
6+
7+
.. facet::
8+
:name: genre
9+
:values: tutorial
10+
11+
.. meta::
12+
:keywords: test connection, runnable, code example
13+
14+
.. procedure::
15+
:style: connected
16+
17+
.. step:: Add a web route to insert data
18+
19+
In the ``app.rb`` file, add a ``post`` route called
20+
``add_restaurant``, as shown in the following code:
21+
22+
.. code-block:: ruby
23+
24+
post '/add_restaurant' do
25+
Restaurant.create!(params[:restaurant])
26+
end
27+
28+
.. step:: Post a request to create a restaurant entry
29+
30+
Send a ``Restaurant`` instance to the ``add_restaurant`` endpoint
31+
by running the following command from the application root
32+
directory:
33+
34+
.. code-block:: bash
35+
36+
curl -d \
37+
'restaurant[name]=Good+Earth+Cafe&restaurant[cuisine]=Cafe&restaurant[borough]=Queens' \
38+
http://localhost:4567/add_restaurant
39+
40+
.. step:: View the data
41+
42+
Refresh http://localhost:4567/list_restaurants in your web browser
43+
to view the new restaurant entry that you submitted. The inserted
44+
restaurant appears at the bottom of the list.
45+
46+
.. include:: /includes/quick-start/troubleshoot.rst

0 commit comments

Comments
 (0)