aboutsummaryrefslogtreecommitdiffstats
path: root/railties/doc/guides/html/command_line.html
blob: 14db786b7873963b5130a33a9b548ba918dc20ee (plain) (blame)
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
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
  <title>A Guide to The Rails Command Line</title>
  <!--[if lt IE 8]>
  <script src="http://ie7-js.googlecode.com/svn/version/2.0(beta3)/IE8.js" type="text/javascript"></script>
  <![endif]-->
  <link href="stylesheets/base.css" media="screen" rel="Stylesheet" type="text/css" />
  <link href="stylesheets/forms.css" media="screen" rel="Stylesheet" type="text/css" />
  <link href="stylesheets/more.css" media="screen" rel="Stylesheet" type="text/css" />
</head>
<body>
  <div id="header" >
    <div id="logo">
      <a href="index.html" title="Ruby on Rails"><img src="images/rails_logo_remix.gif" alt="Rails" height="140" width="110" /></a>
    </div>

    <h1 id="site_title"><span>Ruby on Rails</span></h1>
    <h2 id="site_title_tagline">Sustainable productivity for web-application development</h2>

    <ul id="navMain">
      <li class="first-child"><a href="http://www.rubyonrails.org/" title="Ruby on Rails" class="ruby_on_rails">Ruby on Rails</a></li>
      <li><a class="manuals" href="index.html" title="Manuals Index">Guides Index</a></li>
    </ul>
  </div>

  <div id="container">
    
    <div id="sidebar">
      <h2>Chapters</h2>
      <ol>
          <li>
          <a href="#_command_line_basics">Command Line Basics</a>
            <ul>
            
              <li><a href="#_rails">rails</a></li>
            
              <li><a href="#_server">server</a></li>
            
              <li><a href="#_generate">generate</a></li>
            
              <li><a href="#_console">console</a></li>
            
              <li><a href="#_dbconsole">dbconsole</a></li>
            
              <li><a href="#_plugin">plugin</a></li>
            
              <li><a href="#_runner">runner</a></li>
            
              <li><a href="#_destroy">destroy</a></li>
            
              <li><a href="#_about">about</a></li>
            
            </ul>
          </li>
          <li>
          <a href="#_the_rails_advanced_command_line_racl">The Rails Advanced Command Line (RACL!)</a>
            <ul>
            
              <li><a href="#_rails_with_databases_and_scm">rails with databases and SCM</a></li>
            
            </ul>
          </li>
      </ol>
    </div>
    
    <div id="content">
        <h1>A Guide to The Rails Command Line</h1>
      <div id="preamble">
<div class="sectionbody">
<div class="paragraph"><p>Rails comes with every command line tool you&#8217;ll need to</p></div>
<div class="ulist"><ul>
<li>
<p>
Create a Rails application
</p>
</li>
<li>
<p>
Generate models, controllers, database migrations, and unit tests
</p>
</li>
<li>
<p>
Start a development server
</p>
</li>
<li>
<p>
Mess with objects through an interactive shell
</p>
</li>
<li>
<p>
Profile and benchmark your new creation
</p>
</li>
</ul></div>
<div class="paragraph"><p>... and much, much more! (Buy now!)</p></div>
<div class="paragraph"><p>This tutorial assumes you have basic Rails knowledge from reading the Getting Started with Rails Guide.</p></div>
</div>
</div>
<h2 id="_command_line_basics">1. Command Line Basics</h2>
<div class="sectionbody">
<div class="paragraph"><p>There are a few commands that are absolutely critical to your everyday usage of Rails. In the order of how much you&#8217;ll probably use them are:</p></div>
<div class="ulist"><ul>
<li>
<p>
console
</p>
</li>
<li>
<p>
server
</p>
</li>
<li>
<p>
rake
</p>
</li>
<li>
<p>
generate
</p>
</li>
<li>
<p>
rails
</p>
</li>
</ul></div>
<div class="paragraph"><p>Let&#8217;s create a simple Rails application to step through each of these commands in context.</p></div>
<h3 id="_rails">1.1. rails</h3>
<div class="paragraph"><p>The first thing we&#8217;ll want to do is create a new Rails application by running the <tt>rails</tt> command after installing Rails.</p></div>
<div class="admonitionblock">
<table><tr>
<td class="icon">
<img src="./images/icons/note.png" alt="Note" />
</td>
<td class="content">You know you need the rails gem installed by typing <tt>gem install rails</tt> first, right? Okay, okay, just making sure.</td>
</tr></table>
</div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt>$ rails commandsapp

     create
     create  app/controllers
     create  app/helpers
     create  app/models
     <span style="color: #990000">...</span>
     <span style="color: #990000">...</span>
     create  log/production<span style="color: #990000">.</span>log
     create  log/development<span style="color: #990000">.</span>log
     create  log/test<span style="color: #990000">.</span>log</tt></pre></div></div>
<div class="paragraph"><p>Rails will set you up with what seems like a huge amount of stuff for such a tiny command! You&#8217;ve got the entire Rails directory structure now with all the code you need to run our simple application right out of the box.</p></div>
<div class="admonitionblock">
<table><tr>
<td class="icon">
<img src="./images/icons/note.png" alt="Note" />
</td>
<td class="content">This output will seem very familiar when we get to the <tt>generate</tt> command. Creepy foreshadowing!</td>
</tr></table>
</div>
<h3 id="_server">1.2. server</h3>
<div class="paragraph"><p>Let&#8217;s try it! The <tt>server</tt> command launches a small web server named WEBrick which comes bundled with Ruby. You&#8217;ll use this any time you want to view your work through a web browser.</p></div>
<div class="admonitionblock">
<table><tr>
<td class="icon">
<img src="./images/icons/note.png" alt="Note" />
</td>
<td class="content">WEBrick isn&#8217;t your only option for serving Rails. We&#8217;ll get to that in a later section. [XXX: which section]</td>
</tr></table>
</div>
<div class="paragraph"><p>Without any prodding of any kind, <tt>server</tt> will run our new shiny Rails app:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt>$ cd commandsapp
$ <span style="color: #990000">.</span>/script/server
<span style="color: #990000">=&gt;</span> Booting WEBrick<span style="color: #990000">...</span>
<span style="color: #990000">=&gt;</span> Rails <span style="color: #993399">2.2</span><span style="color: #990000">.</span><span style="color: #993399">0</span> application started on http<span style="color: #990000">://</span><span style="color: #993399">0.0</span><span style="color: #990000">.</span><span style="color: #993399">0.0</span><span style="color: #990000">:</span><span style="color: #993399">3000</span>
<span style="color: #990000">=&gt;</span> Ctrl-C to shutdown server<span style="color: #990000">;</span> call with --help <span style="font-weight: bold"><span style="color: #0000FF">for</span></span> options
<span style="color: #990000">[</span><span style="color: #993399">2008</span>-<span style="color: #993399">11</span>-<span style="color: #993399">04</span> <span style="color: #993399">10</span><span style="color: #990000">:</span><span style="color: #993399">11</span><span style="color: #990000">:</span><span style="color: #993399">38</span><span style="color: #990000">]</span> INFO  WEBrick <span style="color: #993399">1.3</span><span style="color: #990000">.</span><span style="color: #993399">1</span>
<span style="color: #990000">[</span><span style="color: #993399">2008</span>-<span style="color: #993399">11</span>-<span style="color: #993399">04</span> <span style="color: #993399">10</span><span style="color: #990000">:</span><span style="color: #993399">11</span><span style="color: #990000">:</span><span style="color: #993399">38</span><span style="color: #990000">]</span> INFO  ruby <span style="color: #993399">1.8</span><span style="color: #990000">.</span><span style="color: #993399">5</span> <span style="color: #990000">(</span><span style="color: #993399">2006</span>-<span style="color: #993399">12</span>-<span style="color: #993399">04</span><span style="color: #990000">)</span> <span style="color: #990000">[</span>i486-linux<span style="color: #990000">]</span>
<span style="color: #990000">[</span><span style="color: #993399">2008</span>-<span style="color: #993399">11</span>-<span style="color: #993399">04</span> <span style="color: #993399">10</span><span style="color: #990000">:</span><span style="color: #993399">11</span><span style="color: #990000">:</span><span style="color: #993399">38</span><span style="color: #990000">]</span> INFO  WEBrick<span style="color: #990000">::</span>HTTPServer<span style="font-style: italic"><span style="color: #9A1900">#start: pid=18994 port=3000</span></span></tt></pre></div></div>
<div class="paragraph"><p>WHOA. With just three commands we whipped up a Rails server listening on port 3000. Go! Go right now to your browser and go to <a href="http://localhost:3000">http://localhost:3000</a>. I&#8217;ll wait.</p></div>
<div class="paragraph"><p>See? Cool! It doesn&#8217;t do much yet, but we&#8217;ll change that.</p></div>
<h3 id="_generate">1.3. generate</h3>
<div class="paragraph"><p>The <tt>generate</tt> command uses templates to create a whole lot of things. You can always find out what&#8217;s available by running <tt>generate</tt> by itself. Let&#8217;s do that:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt>$ <span style="color: #990000">.</span>/script/generate
Usage<span style="color: #990000">:</span> <span style="color: #990000">.</span>/script/generate generator <span style="color: #990000">[</span>options<span style="color: #990000">]</span> <span style="color: #990000">[</span>args<span style="color: #990000">]</span>

<span style="color: #990000">...</span>
<span style="color: #990000">...</span>

Installed Generators
  Builtin<span style="color: #990000">:</span> controller<span style="color: #990000">,</span> integration_test<span style="color: #990000">,</span> mailer<span style="color: #990000">,</span> migration<span style="color: #990000">,</span> model<span style="color: #990000">,</span> observer<span style="color: #990000">,</span> performance_test<span style="color: #990000">,</span> plugin<span style="color: #990000">,</span> resource<span style="color: #990000">,</span> scaffold<span style="color: #990000">,</span> session_migration

<span style="color: #990000">...</span>
<span style="color: #990000">...</span></tt></pre></div></div>
<div class="admonitionblock">
<table><tr>
<td class="icon">
<img src="./images/icons/note.png" alt="Note" />
</td>
<td class="content">You can install more generators through generator gems, portions of plugins you&#8217;ll undoubtedly install, and you can even create your own!</td>
</tr></table>
</div>
<div class="paragraph"><p>Using generators will save you a large amount of time by writing <strong>boilerplate code</strong> for you&#8201;&#8212;&#8201;necessary for the darn thing to work, but not necessary for you to spend time writing. That&#8217;s what we have computers for, right?</p></div>
<div class="paragraph"><p>Let&#8217;s make our own controller with the controller generator. But what command should we use? Let&#8217;s ask the generator:</p></div>
<div class="admonitionblock">
<table><tr>
<td class="icon">
<img src="./images/icons/note.png" alt="Note" />
</td>
<td class="content">All Rails console utilities have help text. As with most *NIX utilities, you can try adding <tt>--help</tt> or <tt>-h</tt> to the end, for example <tt>./script/server --help</tt>.</td>
</tr></table>
</div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt>$ <span style="color: #990000">.</span>/script/generate controller
Usage<span style="color: #990000">:</span> <span style="color: #990000">.</span>/script/generate controller ControllerName <span style="color: #990000">[</span>options<span style="color: #990000">]</span>

<span style="color: #990000">...</span>
<span style="color: #990000">...</span>

Example<span style="color: #990000">:</span>
    `<span style="color: #990000">.</span>/script/generate controller CreditCard open debit credit close`

    Credit card controller with URLs like /credit_card/debit<span style="color: #990000">.</span>
        Controller<span style="color: #990000">:</span> app/controllers/credit_card_controller<span style="color: #990000">.</span>rb
        Views<span style="color: #990000">:</span>      app/views/credit_card/debit<span style="color: #990000">.</span>html<span style="color: #990000">.</span>erb <span style="color: #990000">[...]</span>
        Helper<span style="color: #990000">:</span>     app/helpers/credit_card_helper<span style="color: #990000">.</span>rb
        Test<span style="color: #990000">:</span>       test/functional/credit_card_controller_test<span style="color: #990000">.</span>rb

Modules Example<span style="color: #990000">:</span>
    `<span style="color: #990000">.</span>/script/generate controller <span style="color: #FF0000">'admin/credit_card'</span> <span style="font-weight: bold"><span style="color: #0000FF">suspend</span></span> late_fee`

    Credit card admin controller with URLs /admin/credit_card/suspend<span style="color: #990000">.</span>
        Controller<span style="color: #990000">:</span> app/controllers/admin/credit_card_controller<span style="color: #990000">.</span>rb
        Views<span style="color: #990000">:</span>      app/views/admin/credit_card/debit<span style="color: #990000">.</span>html<span style="color: #990000">.</span>erb <span style="color: #990000">[...]</span>
        Helper<span style="color: #990000">:</span>     app/helpers/admin/credit_card_helper<span style="color: #990000">.</span>rb
        Test<span style="color: #990000">:</span>       test/functional/admin/credit_card_controller_test<span style="color: #990000">.</span>rb</tt></pre></div></div>
<div class="paragraph"><p>Ah, the controller generator is expecting parameters in the form of <tt>generate controller ControllerName action1 action2</tt>. Let&#8217;s make a <tt>Greetings</tt> controller with an action of <strong>hello</strong>, which will say something nice to us.</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt>$ <span style="color: #990000">.</span>/script/generate controller Greeting hello
     exists  app/controllers<span style="color: #990000">/</span>
     exists  app/helpers<span style="color: #990000">/</span>
     create  app/views/greeting
     exists  test/functional<span style="color: #990000">/</span>
     create  app/controllers/greetings_controller<span style="color: #990000">.</span>rb
     create  test/functional/greetings_controller_test<span style="color: #990000">.</span>rb
     create  app/helpers/greetings_helper<span style="color: #990000">.</span>rb
     create  app/views/greetings/hello<span style="color: #990000">.</span>html<span style="color: #990000">.</span>erb</tt></pre></div></div>
<div class="paragraph"><p>Look there! Now what all did this generate? It looks like it made sure a bunch of directories were in our application, and created a controller file, a functional test file, a helper for the view, and a view file.</p></div>
<div class="paragraph"><p>Let&#8217;s check out the controller and modify it a little (in <tt>app/controllers/greeting_controller.rb</tt>):</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> GreetingController <span style="color: #990000">&lt;</span> ApplicationController
  <span style="font-weight: bold"><span style="color: #0000FF">def</span></span> hello
    <span style="color: #009900">@message</span> <span style="color: #990000">=</span> <span style="color: #FF0000">"Hello, how are you today? I am exuberant!"</span>
  <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>

<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
<div class="paragraph"><p>Then the view, to display our nice message (in <tt>app/views/greeting/hello.html.erb</tt>):</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">&lt;h1&gt;</span></span>A Greeting for You!<span style="font-weight: bold"><span style="color: #0000FF">&lt;/h1&gt;</span></span>
<span style="font-weight: bold"><span style="color: #0000FF">&lt;p&gt;</span></span>&lt;%= @message %&gt;<span style="font-weight: bold"><span style="color: #0000FF">&lt;/p&gt;</span></span></tt></pre></div></div>
<div class="paragraph"><p>Deal. Go check it out in your browser. Fire up your server. Remember? <tt>./script/server</tt> at the root of your Rails application should do it.</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt>$ <span style="color: #990000">.</span>/script/server
<span style="color: #990000">=&gt;</span> Booting WEBrick<span style="color: #990000">...</span></tt></pre></div></div>
<div class="paragraph"><p>The URL will be <tt>http://localhost:3000/greetings/hello</tt>. I&#8217;ll wait for you to be suitably impressed.</p></div>
<div class="admonitionblock">
<table><tr>
<td class="icon">
<img src="./images/icons/note.png" alt="Note" />
</td>
<td class="content">With a normal, plain-old Rails application, your URLs will generally follow the pattern of <a href="http://(host)/(controller)/(action">http://(host)/(controller)/(action</a>), and a URL like <a href="http://(host)/(controller">http://(host)/(controller</a>) will hit the <strong>index</strong> action of that controller.</td>
</tr></table>
</div>
<div class="paragraph"><p>"What about data, though?", you ask over a cup of coffee. Rails comes with a generator for data models too. Can you guess its generator name?</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt>$ <span style="color: #990000">.</span>/script/generate model
Usage<span style="color: #990000">:</span> <span style="color: #990000">.</span>/script/generate model ModelName <span style="color: #990000">[</span>field<span style="color: #990000">:</span><span style="font-weight: bold"><span style="color: #0000FF">type</span></span><span style="color: #990000">,</span> field<span style="color: #990000">:</span><span style="font-weight: bold"><span style="color: #0000FF">type</span></span><span style="color: #990000">]</span>

<span style="color: #990000">...</span>

Examples<span style="color: #990000">:</span>
    `<span style="color: #990000">.</span>/script/generate model account`

        creates an Account model<span style="color: #990000">,</span> <span style="font-weight: bold"><span style="color: #0000FF">test</span></span><span style="color: #990000">,</span> fixture<span style="color: #990000">,</span> and migration<span style="color: #990000">:</span>
            Model<span style="color: #990000">:</span>      app/models/account<span style="color: #990000">.</span>rb
            Test<span style="color: #990000">:</span>       test/unit/account_test<span style="color: #990000">.</span>rb
            Fixtures<span style="color: #990000">:</span>   test/fixtures/accounts<span style="color: #990000">.</span>yml
            Migration<span style="color: #990000">:</span>  db/migrate/XXX_add_accounts<span style="color: #990000">.</span>rb

    `<span style="color: #990000">.</span>/script/generate model post title<span style="color: #990000">:</span>string body<span style="color: #990000">:</span>text published<span style="color: #990000">:</span>boolean`

        creates a Post model with a string title<span style="color: #990000">,</span> text body<span style="color: #990000">,</span> and published flag<span style="color: #990000">.</span></tt></pre></div></div>
<div class="paragraph"><p>But instead of generating a model directly (which we&#8217;ll be doing later), let&#8217;s set up a scaffold. A <strong>scaffold</strong> in Rails is a full set of model, database migration for that model, controller to manipulate it, views to view and manipulate the data, and a test suite for each of the above.</p></div>
<div class="paragraph"><p>Let&#8217;s set up a simple resource called "HighScore" that will keep track of our highest score on video games we play.</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt>$ <span style="color: #990000">.</span>/script/generate scaffold HighScore game<span style="color: #990000">:</span>string score<span style="color: #990000">:</span>integer
    exists  app/models<span style="color: #990000">/</span>
    exists  app/controllers<span style="color: #990000">/</span>
    exists  app/helpers<span style="color: #990000">/</span>
    create  app/views/high_scores
    create  app/views/layouts<span style="color: #990000">/</span>
    exists  test/functional<span style="color: #990000">/</span>
    create  test/unit<span style="color: #990000">/</span>
    create  public/stylesheets<span style="color: #990000">/</span>
    create  app/views/high_scores/index<span style="color: #990000">.</span>html<span style="color: #990000">.</span>erb
    create  app/views/high_scores/show<span style="color: #990000">.</span>html<span style="color: #990000">.</span>erb
    create  app/views/high_scores/new<span style="color: #990000">.</span>html<span style="color: #990000">.</span>erb
    create  app/views/high_scores/edit<span style="color: #990000">.</span>html<span style="color: #990000">.</span>erb
    create  app/views/layouts/high_scores<span style="color: #990000">.</span>html<span style="color: #990000">.</span>erb
    create  public/stylesheets/scaffold<span style="color: #990000">.</span>css
    create  app/controllers/high_scores_controller<span style="color: #990000">.</span>rb
    create  test/functional/high_scores_controller_test<span style="color: #990000">.</span>rb
    create  app/helpers/high_scores_helper<span style="color: #990000">.</span>rb
     route  map<span style="color: #990000">.</span>resources <span style="color: #990000">:</span>high_scores
dependency  model
    exists    app/models<span style="color: #990000">/</span>
    exists    test/unit<span style="color: #990000">/</span>
    create    test/fixtures<span style="color: #990000">/</span>
    create    app/models/high_score<span style="color: #990000">.</span>rb
    create    test/unit/high_score_test<span style="color: #990000">.</span>rb
    create    test/fixtures/high_scores<span style="color: #990000">.</span>yml
    exists    db/migrate
    create    db/migrate<span style="color: #990000">/</span>20081217071914_create_high_scores<span style="color: #990000">.</span>rb</tt></pre></div></div>
<div class="paragraph"><p>Taking it from the top - the generator checks that there exist the directories for models, controllers, helpers, layouts, functional and unit tests, stylesheets, creates the views, controller, model and database migration for HighScore (creating the <tt>high_scores</tt> table and fields), takes care of the route for the <strong>resource</strong>, and new tests for everything.</p></div>
<div class="paragraph"><p>The migration requires that we <strong>migrate</strong>, that is, run some Ruby code (living in that <tt>20081217071914_create_high_scores.rb</tt>) to modify the schema of our database. Which database? The sqlite3 database that Rails will create for you when we run the <tt>rake db:migrate</tt> command. We&#8217;ll talk more about Rake in-depth in a little while.</p></div>
<div class="admonitionblock">
<table><tr>
<td class="icon">
<img src="./images/icons/note.png" alt="Note" />
</td>
<td class="content">Hey. Install the sqlite3-ruby gem while you&#8217;re at it. <tt>gem install sqlite3-ruby</tt></td>
</tr></table>
</div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt>$ rake db<span style="color: #990000">:</span>migrate
<span style="color: #990000">(</span><span style="font-weight: bold"><span style="color: #0000FF">in</span></span> /home/commandsapp<span style="color: #990000">)</span>
<span style="color: #990000">==</span>  CreateHighScores<span style="color: #990000">:</span> migrating <span style="color: #990000">===============================================</span>
-- create_table<span style="color: #990000">(:</span>high_scores<span style="color: #990000">)</span>
   -<span style="color: #990000">&gt;</span> <span style="color: #993399">0</span><span style="color: #990000">.</span>0070s
<span style="color: #990000">==</span>  CreateHighScores<span style="color: #990000">:</span> migrated <span style="color: #990000">(</span><span style="color: #993399">0</span><span style="color: #990000">.</span>0077s<span style="color: #990000">)</span> <span style="color: #990000">======================================</span></tt></pre></div></div>
<div class="admonitionblock">
<table><tr>
<td class="icon">
<img src="./images/icons/note.png" alt="Note" />
</td>
<td class="content">Let&#8217;s talk about unit tests. Unit tests are code that tests and makes assertions about code. In unit testing, we take a little part of code, say a method of a model, and test its inputs and outputs. Unit tests are your friend. The sooner you make peace with the fact that your quality of life will drastically increase when you unit test your code, the better. Seriously. We&#8217;ll make one in a moment.</td>
</tr></table>
</div>
<div class="paragraph"><p>Let&#8217;s see the interface Rails created for us. ./script/server; <a href="http://localhost:3000/high_scores">http://localhost:3000/high_scores</a></p></div>
<div class="paragraph"><p>We can create new high scores (55,160 on Space Invaders!)</p></div>
<h3 id="_console">1.4. console</h3>
<div class="paragraph"><p>The <tt>console</tt> command lets you interact with your Rails application from the command line. On the underside, <tt>script/console</tt> uses IRB, so if you&#8217;ve ever used it, you&#8217;ll be right at home. This is useful for testing out quick ideas with code and changing data server-side without touching the website.</p></div>
<h3 id="_dbconsole">1.5. dbconsole</h3>
<div class="paragraph"><p><tt>dbconsole</tt> figures out which database you&#8217;re using and drops you into whichever command line interface you would use with it (and figures out the command line parameters to give to it, too!). It supports MySQL, PostgreSQL, SQLite and SQLite3.</p></div>
<h3 id="_plugin">1.6. plugin</h3>
<div class="paragraph"><p>The <tt>plugin</tt> command simplifies plugin management; think a miniature version of the Gem utility. Let&#8217;s walk through installing a plugin. You can call the sub-command <strong>discover</strong>, which sifts through repositories looking for plugins, or call <strong>source</strong> to add a specific repository of plugins, or you can specify the plugin location directly.</p></div>
<div class="paragraph"><p>Let&#8217;s say you&#8217;re creating a website for a client who wants a small accounting system. Every event having to do with money must be logged, and must never be deleted. Wouldn&#8217;t it be great if we could override the behavior of a model to never actually take its record out of the database, but <strong>instead</strong>, just set a field?</p></div>
<div class="paragraph"><p>There is such a thing! The plugin we&#8217;re installing is called "acts_as_paranoid", and it lets models implement a "deleted_at" column that gets set when you call destroy. Later, when calling find, the plugin will tack on a database check to filter out "deleted" things.</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt>$ <span style="color: #990000">.</span>/script/plugin install http<span style="color: #990000">:</span>//svn<span style="color: #990000">.</span>techno-weenie<span style="color: #990000">.</span>net/projects/plugins/acts_as_paranoid
<span style="color: #990000">+</span> <span style="color: #990000">.</span>/CHANGELOG
<span style="color: #990000">+</span> <span style="color: #990000">.</span>/MIT-LICENSE
<span style="color: #990000">...</span>
<span style="color: #990000">...</span></tt></pre></div></div>
<h3 id="_runner">1.7. runner</h3>
<div class="paragraph"><p><tt>runner</tt> runs Ruby code in the context of Rails non-interactively. For instance:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt>$ <span style="color: #990000">.</span>/script/runner <span style="color: #FF0000">"Model.long_running_method"</span></tt></pre></div></div>
<h3 id="_destroy">1.8. destroy</h3>
<div class="paragraph"><p>Think of <tt>destroy</tt> as the opposite of <tt>generate</tt>. It&#8217;ll figure out what generate did, and undo it. Believe you-me, the creation of this tutorial used this command many times!</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt>$ <span style="color: #990000">.</span>/script/generate model Oops
      exists  app/models<span style="color: #990000">/</span>
      exists  test/unit<span style="color: #990000">/</span>
      exists  test/fixtures<span style="color: #990000">/</span>
      create  app/models/oops<span style="color: #990000">.</span>rb
      create  test/unit/oops_test<span style="color: #990000">.</span>rb
      create  test/fixtures/oops<span style="color: #990000">.</span>yml
      exists  db/migrate
      create  db/migrate<span style="color: #990000">/</span>20081221040817_create_oops<span style="color: #990000">.</span>rb
$ <span style="color: #990000">.</span>/script/destroy model Oops
    notempty  db/migrate
    notempty  db
          rm  db/migrate<span style="color: #990000">/</span>20081221040817_create_oops<span style="color: #990000">.</span>rb
          rm  test/fixtures/oops<span style="color: #990000">.</span>yml
          rm  test/unit/oops_test<span style="color: #990000">.</span>rb
          rm  app/models/oops<span style="color: #990000">.</span>rb
    notempty  test/fixtures
    notempty  <span style="font-weight: bold"><span style="color: #0000FF">test</span></span>
    notempty  test/unit
    notempty  <span style="font-weight: bold"><span style="color: #0000FF">test</span></span>
    notempty  app/models
    notempty  app</tt></pre></div></div>
<h3 id="_about">1.9. about</h3>
<div class="paragraph"><p>Check it: Version numbers for Ruby, RubyGems, Rails, the Rails subcomponents, your application&#8217;s folder, the current Rails environment name, your app&#8217;s database adapter, and schema version! <tt>about</tt> is useful when you need to ask for help, check if a security patch might affect you, or when you need some stats for an existing Rails installation.</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt>$ <span style="color: #990000">.</span>/script/about
About your application<span style="color: #FF0000">'s environment</span>
<span style="color: #FF0000">Ruby version              1.8.6 (i486-linux)</span>
<span style="color: #FF0000">RubyGems version          1.3.1</span>
<span style="color: #FF0000">Rails version             2.2.0</span>
<span style="color: #FF0000">Active Record version     2.2.0</span>
<span style="color: #FF0000">Action Pack version       2.2.0</span>
<span style="color: #FF0000">Active Resource version   2.2.0</span>
<span style="color: #FF0000">Action Mailer version     2.2.0</span>
<span style="color: #FF0000">Active Support version    2.2.0</span>
<span style="color: #FF0000">Edge Rails revision       unknown</span>
<span style="color: #FF0000">Application root          /home/commandsapp</span>
<span style="color: #FF0000">Environment               development</span>
<span style="color: #FF0000">Database adapter          sqlite3</span>
<span style="color: #FF0000">Database schema version   20081217073400</span></tt></pre></div></div>
</div>
<h2 id="_the_rails_advanced_command_line_racl">2. The Rails Advanced Command Line (RACL!)</h2>
<div class="sectionbody">
<div class="paragraph"><p>The more advanced uses of the command line are focused around finding useful (even surprising at times) options in the utilities, and fitting utilities to your needs and specific work flow. Listed here are some tricks up Rails' sleeve.</p></div>
<h3 id="_rails_with_databases_and_scm">2.1. rails with databases and SCM</h3>
<div class="paragraph"><p>When creating a new Rails application, you have the option to specify what kind of database and what kind of source code management system your application is going to use. This will save you a few minutes, and certainly many keystrokes.</p></div>
<div class="paragraph"><p>Let&#8217;s see what a <tt>--git</tt> option and a <tt>--database=postgresql</tt> option will do for us:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt>$ mkdir gitapp
$ cd gitapp
$ git init
Initialized empty Git repository <span style="font-weight: bold"><span style="color: #0000FF">in</span></span> <span style="color: #990000">.</span>git<span style="color: #990000">/</span>
$ rails <span style="color: #990000">.</span> --git --database<span style="color: #990000">=</span>postgresql
      exists
      create  app/controllers
      create  app/helpers
<span style="color: #990000">...</span>
<span style="color: #990000">...</span>
      create  tmp/cache
      create  tmp/pids
      create  Rakefile
add <span style="color: #FF0000">'Rakefile'</span>
      create  README
add <span style="color: #FF0000">'README'</span>
      create  app/controllers/application<span style="color: #990000">.</span>rb
add <span style="color: #FF0000">'app/controllers/application.rb'</span>
      create  app/helpers/application_helper<span style="color: #990000">.</span>rb
<span style="color: #990000">...</span>
      create  log/test<span style="color: #990000">.</span>log
add <span style="color: #FF0000">'log/test.log'</span></tt></pre></div></div>
<div class="paragraph"><p>We had to create the <strong>gitapp</strong> directory and initialize an empty git repository before Rails would add files it created to our repository. Let&#8217;s see what it put in our database configuration:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt>$ cat config/database<span style="color: #990000">.</span>yml
<span style="font-style: italic"><span style="color: #9A1900"># PostgreSQL. Versions 7.4 and 8.x are supported.</span></span>
<span style="font-style: italic"><span style="color: #9A1900">#</span></span>
<span style="font-style: italic"><span style="color: #9A1900"># Install the ruby-postgres driver:</span></span>
<span style="font-style: italic"><span style="color: #9A1900">#   gem install ruby-postgres</span></span>
<span style="font-style: italic"><span style="color: #9A1900"># On Mac OS X:</span></span>
<span style="font-style: italic"><span style="color: #9A1900">#   gem install ruby-postgres -- --include=/usr/local/pgsql</span></span>
<span style="font-style: italic"><span style="color: #9A1900"># On Windows:</span></span>
<span style="font-style: italic"><span style="color: #9A1900">#   gem install ruby-postgres</span></span>
<span style="font-style: italic"><span style="color: #9A1900">#       Choose the win32 build.</span></span>
<span style="font-style: italic"><span style="color: #9A1900">#       Install PostgreSQL and put its /bin directory on your path.</span></span>
development<span style="color: #990000">:</span>
  adapter<span style="color: #990000">:</span> postgresql
  encoding<span style="color: #990000">:</span> unicode
  database<span style="color: #990000">:</span> gitapp_development
  pool<span style="color: #990000">:</span> <span style="color: #993399">5</span>
  username<span style="color: #990000">:</span> gitapp
  password<span style="color: #990000">:</span>
<span style="color: #990000">...</span>
<span style="color: #990000">...</span></tt></pre></div></div>
<div class="paragraph"><p>It also generated some lines in our database.yml configuration corresponding to our choice of PostgreSQL for database. The only catch with using the SCM options is that you have to make your application&#8217;s directory first, then initialize your SCM, then you can run the <tt>rails</tt> command to generate the basis of your app.</p></div>
</div>

    </div>
  </div>
</body>
</html>