-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathrails_with_scaffold.html
More file actions
457 lines (445 loc) · 28.5 KB
/
rails_with_scaffold.html
File metadata and controls
457 lines (445 loc) · 28.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
<!doctype html>
<html dir="ltr" lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Rails with Scaffold — Ruby on Rails Guides</title>
<link rel="stylesheet" type="text/css" href="stylesheets/style.css" data-turbo-track="reload">
<link rel="stylesheet" type="text/css" href="stylesheets/print.css" media="print">
<link rel="stylesheet" type="text/css" href="stylesheets/highlight.css" data-turbo-track="reload">
<link rel="icon" href="images/backend-development.svg" sizes="any">
<script src="javascripts/@hotwired--turbo.js" data-turbo-track="reload"></script>
<script src="javascripts/clipboard.js" data-turbo-track="reload"></script>
<script src="javascripts/guides.js" data-turbo-track="reload"></script>
<meta property="og:title" content="Rails with Scaffold — Ruby on Rails Guides" />
<meta name="description" content="Rails with ScaffoldWe will build our first rails project but we will not really understand everything - that comes later.After finishing this guide you will have finished your first rails project know how to use the scaffold generator know about the main folders of a rails project: config, db, app have an overview of what model, view, controller (MVC) means in rails" />
<meta property="og:description" content="Rails with ScaffoldWe will build our first rails project but we will not really understand everything - that comes later.After finishing this guide you will have finished your first rails project know how to use the scaffold generator know about the main folders of a rails project: config, db, app have an overview of what model, view, controller (MVC) means in rails" />
<meta property="og:locale" content="en_US" />
<meta property="og:site_name" content="Textbook Backend Developemnt" />
<meta property="og:image" content="images/backend-development.svg" />
<meta property="og:type" content="website" />
<link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link href="https://fonts.googleapis.com/css2?family=Noto+Sans+Arabic:wght@100..900&display=swap" rel="stylesheet">
<link href="https://fonts.googleapis.com/css2?family=Heebo:wght@100..900&family=Noto+Sans+Arabic:wght@100..900&display=swap" rel="stylesheet">
<meta name="theme-color" content="#2e56e9">
</head>
<body class="guide">
<header id="page_header">
<div class="wrapper clearfix">
<nav id="feature_nav">
<div class="header-logo">
<a href="/">Backend Development</a>
</div>
<ul class="nav">
<li><a class="nav-item" id="home_nav" href="/">Home</a></li>
<li class="guides-index guides-index-large">
<a href="index.html" id="guidesMenu" class="guides-index-item nav-item">Index</a>
<div id="guides" class="clearfix" style="display: none;">
<hr />
<dl class="guides-section-container">
<div class="guides-section">
<dt>Ruby on Rails</dt>
<dd><a href="ruby_commandline.html">Ruby Commandline</a></dd>
<dd><a href="rails_database_and_model.html">Models and ActiveRecord</a></dd>
<dd><a href="rails_db.html">Database and Migrations</a></dd>
<dd><a href="rails_associations_and_validations.html">Associations and Validations</a></dd>
<dd><a href="rails_view_and_controller.html">Routing, View and Controller</a></dd>
<dd><a href="rails_authentication.html">Simple Authentication</a></dd>
<dd><a href="assets_and_import_map.html">The Asset Pipeline and Importmaps</a></dd>
<dd><a href="testing.html">Getting started with Testing</a></dd>
<dd><a href="refactoring_rails.html">Refactoring Rails</a></dd>
<dd><a href="deploy-to-paas.html">Deploy to PAAS</a></dd>
<dd><a href="rails_gems.html">Ruby Gems for your Rails Project</a></dd>
<dd><a href="deploying_rails.html">Deploying Rails</a></dd>
</div>
<div class="guides-section">
<dt>Ruby on Rails - Advanced Topics</dt>
<dd><a href="deploy-to-paas.html">Deploy to PAAS</a></dd>
<dd><a href="rest-api.html">REST API</a></dd>
<dd><a href="graphql-api.html">GraphQL API</a></dd>
<dd><a href="rails_websockets.html">Websocket in Rails</a></dd>
<dd><a href="jobs_and_tasks.html">Jobs and Tasks in Rails</a></dd>
<dd><a href="rails_security.html">Rails Security</a></dd>
</div>
<div class="guides-section">
<dt>Overarching Concerns</dt>
<dd><a href="issue.html">Issue Lifecycle</a></dd>
<dd><a href="security.html">Security</a></dd>
<dd><a href="adv_authentication.html">Advanced Authentication</a></dd>
<dd><a href="caching.html">Caching</a></dd>
<dd><a href="advanced_testing.html">Advanced Testing</a></dd>
<dd><a href="internationalization.html">Internationalization (I18n)</a></dd>
<dd><a href="git_rebasing.html">Git Rebasing</a></dd>
</div>
<div class="guides-section">
<dt>Nodes.js</dt>
<dd><a href="node_vs_rails.html">Node vs. Rails</a></dd>
<dd><a href="node_basics.html">Node Basics</a></dd>
<dd><a href="node_websockets.html">Node Websockets</a></dd>
<dd><a href="node_express.html">Node Web App</a></dd>
<dd><a href="node_cluster.html">Scaling Node</a></dd>
</div>
<div class="guides-section">
<dt>Next.js</dt>
<dd><a href="nextjs.html">Next.js</a></dd>
</div>
</dl>
</div>
</li>
<li class="guides-index guides-index-small">
<select class="guides-index-item nav-item">
<option value="index.html">Index</option>
<optgroup label="Ruby on Rails">
<option value="ruby_commandline.html">Ruby Commandline</option>
<option value="rails_database_and_model.html">Models and ActiveRecord</option>
<option value="rails_db.html">Database and Migrations</option>
<option value="rails_associations_and_validations.html">Associations and Validations</option>
<option value="rails_view_and_controller.html">Routing, View and Controller</option>
<option value="rails_authentication.html">Simple Authentication</option>
<option value="assets_and_import_map.html">The Asset Pipeline and Importmaps</option>
<option value="testing.html">Getting started with Testing</option>
<option value="refactoring_rails.html">Refactoring Rails</option>
<option value="deploy-to-paas.html">Deploy to PAAS</option>
<option value="rails_gems.html">Ruby Gems for your Rails Project</option>
<option value="deploying_rails.html">Deploying Rails</option>
</optgroup>
<optgroup label="Ruby on Rails - Advanced Topics">
<option value="deploy-to-paas.html">Deploy to PAAS</option>
<option value="rest-api.html">REST API</option>
<option value="graphql-api.html">GraphQL API</option>
<option value="rails_websockets.html">Websocket in Rails</option>
<option value="jobs_and_tasks.html">Jobs and Tasks in Rails</option>
<option value="rails_security.html">Rails Security</option>
</optgroup>
<optgroup label="Overarching Concerns">
<option value="issue.html">Issue Lifecycle</option>
<option value="security.html">Security</option>
<option value="adv_authentication.html">Advanced Authentication</option>
<option value="caching.html">Caching</option>
<option value="advanced_testing.html">Advanced Testing</option>
<option value="internationalization.html">Internationalization (I18n)</option>
<option value="git_rebasing.html">Git Rebasing</option>
</optgroup>
<optgroup label="Nodes.js">
<option value="node_vs_rails.html">Node vs. Rails</option>
<option value="node_basics.html">Node Basics</option>
<option value="node_websockets.html">Node Websockets</option>
<option value="node_express.html">Node Web App</option>
<option value="node_cluster.html">Scaling Node</option>
</optgroup>
<optgroup label="Next.js">
<option value="nextjs.html">Next.js</option>
</optgroup>
</select>
</li>
</ul>
</nav>
</div>
</header>
<hr class="hide" />
<section id="feature">
<div class="wrapper">
<h1>Rails with Scaffold</h1><p>We will build our first rails project but we will not really understand
everything - that comes later.</p><p>After finishing this guide you will</p>
<ul>
<li>have finished your first rails project</li>
<li>know how to use the scaffold generator</li>
<li>know about the main folders of a rails project: <code>config</code>, <code>db</code>, <code>app</code></li>
<li>have an overview of what model, view, controller (MVC) means in rails</li>
</ul>
<nav id="subCol">
<h3 class="chapter">
<picture>
<!-- Using the `source` HTML tag to set the dark theme image -->
<source
srcset="images/icon_book-close-bookmark-1-wht.svg"
media="(prefers-color-scheme: dark)"
/>
<img src="images/icon_book-close-bookmark-1.svg" alt="Chapter Icon" />
</picture>
Chapters
</h3>
<ol class="chapters">
<li><a href="#rails-from-the-outside-in">Rails from the Outside In</a>
<ul>
<li><a href="#start-a-rails-project">Start a Rails Project</a></li>
<li><a href="#rails-directory-structure">Rails Directory Structure</a></li>
<li><a href="#start-the-webserver">Start the Webserver</a></li>
</ul></li>
<li><a href="#make-it-a-git-repository">Make it a Git Repository</a></li>
<li><a href="#scaffold">Scaffold</a>
<ul>
<li><a href="#migration">Migration</a></li>
<li><a href="#use-your-app">Use your app</a></li>
<li><a href="#view">View</a></li>
<li><a href="#model">Model</a></li>
<li><a href="#summary">Summary</a></li>
</ul></li>
</ol>
</nav>
<hr>
</div>
</section>
<main id="container">
<div class="wrapper">
<div id="mainCol">
<div class='slide'>
</div>
<div class='slide'>
<p class='slide_break_block'><a class='slide_break' id='slide-0' href='slides_rails_with_scaffold.html#/0'>◻</a></p>
<h2 id="rails-from-the-outside-in"><a class="anchorlink" href="#rails-from-the-outside-in"><span>1</span> Rails from the Outside In</a></h2><p>Let's look at a fairly complex web application - the course
management system for a university. How would the code for this
app be organized, if it is written in Ruby on Rails?</p><p>The course management system surely deals with courses in some
way.</p><p>Rails is organized along the distinction of model - view - controller (MVC).
In our exmple we would find:</p>
<ul>
<li>an entry in <code>conf/route.rb</code> that configures the mapping of URLs and parameters to the controller</li>
<li>several templates for courses in the folder <code>app/views/courses/</code></li>
<li>a ruby class in the <code>apps/controllers</code> folder with the class name <code>CourseController</code></li>
<li>a ruby class in the <code>app/models</code> folder with the class name <code>Course</code></li>
<li>a relational database in the backend. this database contains a table <code>courses</code></li>
</ul>
<p>As a rails programmer you know about all these folders and files. If you are
told that there is a problem in the webpage with url <a href="http://myapp.com/courses/5/edit">http://myapp.com/courses/5/edit</a>
you immediately
know to look in conf/routes.rb to find out exactly which files are concerned,
but that the most likely canditates are</p>
<ul>
<li>app/models/course.rb</li>
<li>app/controller/course_controller.b</li>
<li>app/views/course/edit.html.erb</li>
</ul>
<p>If you have not used a framework before this might feel very restrictive in the
beginning: you can't just make up filenames any way you want, there is a
<strong>convention</strong> for everything.</p><p>You have to stick to the convention if you want to profit from the framework,
it will make your work as a programmer a lot easier.
And you will profit from this convention
every time you look at a new project - you'll instantly know your way around.</p></div>
<div class='slide'>
<p class='slide_break_block'><a class='slide_break' id='slide-1' href='slides_rails_with_scaffold.html#/1'>◻</a></p>
<h3 id="start-a-rails-project"><a class="anchorlink" href="#start-a-rails-project"><span>1.1</span> Start a Rails Project</a></h3><p>Before you can start your first rails project you have
to install ruby and the rails gem on your computer.</p><p>A word of warning: Ruby and Rails will work on any Unix, including Mac OS.
To use them on a windows system you need a lot more patience and troubleshooting
ability. So if you are a windows user you might consider running
ruby and rails in a virtual (unix) machine (without GUI) instead.</p><p>Make sure you are using a current ruby (>= 2.3) and rails (>= 5.0) before
you proceed:</p><div class="interstitial code">
<pre><code class="highlight sh"><span class="nv">$ </span><span class="nb">ruby</span> <span class="nt">-v</span>
<span class="nb">ruby </span>2.3.1p112 <span class="o">(</span>2016-04-26 revision 54768<span class="o">)</span> <span class="o">[</span>x86_64-darwin15]
<span class="nv">$ </span><span class="nb">rails</span> <span class="nt">-v</span>
Rails 5.0.0.1
</code></pre>
<button class="clipboard-button" data-clipboard-text="$ ruby -v
ruby 2.3.1p112 (2016-04-26 revision 54768) [x86_64-darwin15]
$ rails -v
Rails 5.0.0.1
">Copy</button>
</div>
<p>Another word of warning: rails moves fast. This textbook first written in the fall
of 2012 for rails 3.2, and has been updated every year until fall 2016.
If you are reading this in the far future (which in rails terms means: in late 1017 or later)
you are probably using ruby 3 or later, and rails 6 or later, and this text is <strong>not</strong> for you!</p><p>Before you start your rails project you have to decide on the name (and folder
name) of your application. The name of application cannot easily be changed
afterwards! In the following example we create an app called 'alljokes':</p><div class="interstitial code">
<pre><code class="highlight sh"><span class="nb">rails </span>new alljokes <span class="nt">-T</span>
</code></pre>
<button class="clipboard-button" data-clipboard-text="rails new alljokes -T
">Copy</button>
</div>
<p>The last step that was run automatically when creating a new rails project is <code>bundle install</code>.
This will try to look up gems on the internet - if you are not connected
to the internet you will be stuck here. You could try <code>bundle install --local</code> instead,
that might help if the gems are installed on your computer already.</p></div>
<div class='slide'>
<p class='slide_break_block'><a class='slide_break' id='slide-2' href='slides_rails_with_scaffold.html#/2'>◻</a></p>
<h3 id="rails-directory-structure"><a class="anchorlink" href="#rails-directory-structure"><span>1.2</span> Rails Directory Structure</a></h3><p>Rails will create a directory structure and about 40 files for you.
Let's start to look at a few of them:</p>
<ul>
<li><code>Gemfile</code> - in this file you specify which libraries (gems) your project uses</li>
<li><code>app</code>
<ul>
<li><code>model</code> - contains all the classes of models</li>
<li><code>view</code> - contains a folder full of templates for every controller</li>
<li><code>controller</code> - contains all the classes of controllers</li>
</ul></li>
<li><code>config</code>
<ul>
<li><code>database.yml</code> - database configuration</li>
<li><code>routes.rb</code> - configuration of routes</li>
</ul></li>
<li><code>public</code> - the webspace. files in here are accessible without routing</li>
</ul>
<p>With your code split up into so many different files it is really useful
to haven an editor that not only helps you edit a single file, but that
will also display the directories and files. For example vim with NERDtree
or submline or RubyMine:</p><p><img src="images/directory-structure-editors.png" alt="Screenshot of editor vim with NERDtree and rubymine"></p></div>
<div class='slide'>
<p class='slide_break_block'><a class='slide_break' id='slide-3' href='slides_rails_with_scaffold.html#/3'>◻</a></p>
<h3 id="start-the-webserver"><a class="anchorlink" href="#start-the-webserver"><span>1.3</span> Start the Webserver</a></h3><p>Rails comes with a tiny webserver called <code>puma</code>. You start
it in your terminal window (and then you need another window to go on working)</p><div class="interstitial code">
<pre><code class="highlight sh"><span class="nv">$ </span><span class="nb">rails </span>server
</code></pre>
<button class="clipboard-button" data-clipboard-text="$ rails server
">Copy</button>
</div>
<p>Now you can point your browser at <code>http://localhost:3000/</code> to find the first
webpage of your app. It's just a dummy page that will vanish once
you add your own pages.</p></div>
<div class='slide'>
<p class='slide_break_block'><a class='slide_break' id='slide-4' href='slides_rails_with_scaffold.html#/4'>◻</a></p>
<h2 id="make-it-a-git-repository"><a class="anchorlink" href="#make-it-a-git-repository"><span>2</span> Make it a Git Repository</a></h2><p>A new rails project is already prepared to be turned
into a git repository: there is a <code>.gitignore</code> file in the main
folder. (If you are using rubymine you could add <code>/.idea</code> to
your <code>.gitignore</code> - that's the directory where rubymine stores
it's configuration. You probably want to keep that private to
each developer.</p><p>Create a new repository and commit in the current status:</p><div class="interstitial code">
<pre><code class="highlight sh"><span class="nv">$ </span><span class="nb">git </span>init <span class="nb">.</span>
Initialized empty Git repository <span class="k">in</span> /Developer/alljokes/.git/
<span class="nv">$ </span><span class="nb">git </span>add <span class="nb">.</span>
<span class="nv">$ </span><span class="nb">git </span>commit <span class="nt">-m</span> <span class="s1">'empty rails project'</span>
<span class="o">[</span>master <span class="o">(</span>root-commit<span class="o">)</span> 2b2053c] empty <span class="nb">rails </span>project
66 files changed, 1881 insertions<span class="o">(</span>+<span class="o">)</span>
create mode 100644 .gitignore
..
create mode 100644 vendor/assets/stylesheets/.gitkeep
create mode 100644 vendor/plugins/.gitkeep
</code></pre>
<button class="clipboard-button" data-clipboard-text="$ git init .
Initialized empty Git repository in /Developer/alljokes/.git/
$ git add .
$ git commit -m 'empty rails project'
[master (root-commit) 2b2053c] empty rails project
66 files changed, 1881 insertions(+)
create mode 100644 .gitignore
..
create mode 100644 vendor/assets/stylesheets/.gitkeep
create mode 100644 vendor/plugins/.gitkeep
">Copy</button>
</div>
<p>From now on you should <code>commit</code> your changes after every
finished step of programming.</p></div>
<div class='slide'>
<p class='slide_break_block'><a class='slide_break' id='slide-5' href='slides_rails_with_scaffold.html#/5'>◻</a></p>
<h2 id="scaffold"><a class="anchorlink" href="#scaffold"><span>3</span> Scaffold</a></h2><p>A scaffold helps you build something. In Rails a scaffold
helps you generate code according to the conventions.</p><p>In our application we want to store jokes. Let's generate
the scaffold for that: We want a model called <code>joke</code> with
two attributes: a (short) title and a (longer) fulltext.</p><p>When we call the scaffold generator on the commandline
we need to specify the name of the model, and the names (and types)
of the attributes. short strings / varchars do not need a type at all.</p><div class="interstitial code">
<pre><code class="highlight sh"><span class="nv">$ </span><span class="nb">rails </span>generate scaffold joke title fulltext:text
invoke active_record
create db/migrate/20161112171526_create_jokes.rb
create app/models/joke.rb
</code></pre>
<button class="clipboard-button" data-clipboard-text="$ rails generate scaffold joke title fulltext:text
invoke active_record
create db/migrate/20161112171526_create_jokes.rb
create app/models/joke.rb
">Copy</button>
</div>
<p>This will generate about 30 lines of output, and create 15 new files.
We will work through the files we need step by step:</p></div>
<div class='slide'>
<p class='slide_break_block'><a class='slide_break' id='slide-6' href='slides_rails_with_scaffold.html#/6'>◻</a></p>
<h3 id="migration"><a class="anchorlink" href="#migration"><span>3.1</span> Migration</a></h3><p>The first file we have to look at is stored in <code>db/migrate</code>. The filename
will be different from the one shown above, because it contains a timestamp.</p><p>Inside the file you will find</p><div class="interstitial code">
<pre><code class="highlight ruby"><span class="k">class</span> <span class="nc">CreateJokes</span> <span class="o"><</span> <span class="no">ActiveRecord</span><span class="o">::</span><span class="no">Migration</span><span class="p">[</span><span class="mf">5.0</span><span class="p">]</span>
<span class="k">def</span> <span class="nf">change</span>
<span class="n">create_table</span> <span class="ss">:jokes</span> <span class="k">do</span> <span class="o">|</span><span class="n">t</span><span class="o">|</span>
<span class="n">t</span><span class="p">.</span><span class="nf">string</span> <span class="ss">:title</span>
<span class="n">t</span><span class="p">.</span><span class="nf">text</span> <span class="ss">:fulltext</span>
<span class="n">t</span><span class="p">.</span><span class="nf">timestamps</span>
<span class="k">end</span>
<span class="k">end</span>
<span class="k">end</span>
</code></pre>
<button class="clipboard-button" data-clipboard-text="class CreateJokes < ActiveRecord::Migration[5.0]
def change
create_table :jokes do |t|
t.string :title
t.text :fulltext
t.timestamps
end
end
end
">Copy</button>
</div>
<p>This is ruby code to generate a database table. You can see the
two attrivbutes you specified when you called the generator. There is
also a line <code>t.timestamps</code> that will add two more attributes to the
table: <code>created_at</code> and <code>updated_at</code>. Rails handles these to values
automatically.</p><p>You can run the migration (tell ruby to actually create the table) by
typing in <code>rails db:migrate</code> on the command line:</p><div class="interstitial code">
<pre><code class="highlight sh"><span class="nv">$ </span><span class="nb">rails </span>db:migrate
<span class="o">==</span> 20161112171526 CreateJokes: migrating <span class="o">======</span>
<span class="nt">--</span> create_table<span class="o">(</span>:jokes<span class="o">)</span>
-> 0.0012s
<span class="o">==</span> 20161112171526 CreateJokes: migrated <span class="o">(</span>0.0013s<span class="o">)</span> <span class="o">===========</span>
</code></pre>
<button class="clipboard-button" data-clipboard-text="$ rails db:migrate
== 20161112171526 CreateJokes: migrating ======
-- create_table(:jokes)
-> 0.0012s
== 20161112171526 CreateJokes: migrated (0.0013s) ===========
">Copy</button>
</div>
<p>This actually created the table in a sqlite3 database called
<code>db/development.sqlite3</code>. As a side effect it also dumped the current
database schema into <code>db/schema.rb</code> and created a two extra tables
<code>schema_migrations</code> and <code>ar_internal_metadata</code>.</p></div>
<div class='slide'>
<p class='slide_break_block'><a class='slide_break' id='slide-7' href='slides_rails_with_scaffold.html#/7'>◻</a></p>
<h3 id="use-your-app"><a class="anchorlink" href="#use-your-app"><span>3.2</span> Use your app</a></h3><p>After running the migration your app is ready to be used:
point your browser at <code>http://localhost:3000/jokes/</code> to start.</p><p>The scaffold generated four webpages that you can visit, to
list, show, create, edit and destroy jokes:</p><p><img src="images/scaffold-with-arrows.png?viewbox=0;0;900;820" alt="webpages created by the scaffold"></p>
<ul>
<li><code>/jokes/</code> is a list of all the jokes, with links to create, edit and destroy them</li>
<li><code>/jokes/1</code> shows a single joke, in this case the joke with id=1</li>
<li><code>/jokes/new</code> shows a form used to enter a new joke</li>
<li><code>/jokes/1/edit</code> shows a form to edit a joke, in this case the joke with id=1</li>
</ul>
</div>
<div class='slide'>
<p class='slide_break_block'><a class='slide_break' id='slide-8' href='slides_rails_with_scaffold.html#/8'>◻</a></p>
<h3 id="view"><a class="anchorlink" href="#view"><span>3.3</span> View</a></h3><p>You can find the views that correspond to the webpages in <code>app/views/jokes</code>. Try to add a bit
of html to the following two views:</p>
<ul>
<li><code>index.html.erb</code> is a list of all the jokes, with links to create, edit and destroy them</li>
<li><code>show.html.erb</code> shows a single joke</li>
</ul>
</div>
<div class='slide'>
<p class='slide_break_block'><a class='slide_break' id='slide-9' href='slides_rails_with_scaffold.html#/9'>◻</a></p>
<h3 id="model"><a class="anchorlink" href="#model"><span>3.4</span> Model</a></h3><p>The model is stored in <code>app/models</code>. Add the following validation:</p><div class="interstitial code">
<pre><code class="highlight ruby"><span class="k">class</span> <span class="nc">Joke</span> <span class="o"><</span> <span class="no">ApplicationRecord</span>
<span class="n">validates</span> <span class="ss">:title</span><span class="p">,</span> <span class="ss">presence: </span><span class="kp">true</span>
<span class="k">end</span>
</code></pre>
<button class="clipboard-button" data-clipboard-text="class Joke < ApplicationRecord
validates :title, presence: true
end
">Copy</button>
</div>
<p>This tell the model not only accept jokes that have a title.</p><p>Now try to enter a new joke without a title, you should get a
error message:</p><p><img src="images/error-validation.png" alt="Error message caused by unfullfilled validation"></p></div>
<div class='slide'>
<p class='slide_break_block'><a class='slide_break' id='slide-10' href='slides_rails_with_scaffold.html#/10'>◻</a></p>
<h3 id="summary"><a class="anchorlink" href="#summary"><span>3.5</span> Summary</a></h3><p>We built a first rails app and ran it locally on our own machine.
You should now have an overview over the most important files
and a first impression of ruby code (used in controllers, models)
and embedded ruby (erb) used in the views.</p><p>Congratulations on your first step!</p></div>
</div>
</div>
</main>
<hr class="hide" />
<footer id="page_footer">
<div class="wrapper">
<p class="copyright">published under <a href="https://creativecommons.org/licenses/by-nc-sa/3.0/at/deed.de">creative commons by-nc-sa</a> in 2012-2025 by <a href="https://brigitte-jellinek.at">Brigitte Jellinek</a>.
</p>
<p>If you want to contribute: <a href="https://github.com/backend-development/backend-development-textbook/fork">fork the source on github</a>
</p>
<p>Favicon "backend development" by Arkinasi from <a href="https://thenounproject.com/browse/icons/term/backend-development/" target="_blank" title="backend development Icons">Noun Project</a> (CC BY 3.0)</p>
</div>
</footer>
</body>
</html>