|
| 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 |
0 commit comments