Froze rails gems
[depot.git] / vendor / rails / railties / doc / guides / source / command_line.txt
1 A Guide to The Rails Command Line
2 =================================
3
4 Rails comes with every command line tool you'll need to
5
6 * Create a Rails application
7 * Generate models, controllers, database migrations, and unit tests
8 * Start a development server
9 * Mess with objects through an interactive shell
10 * Profile and benchmark your new creation
11
12 ... and much, much more! (Buy now!)
13
14 This tutorial assumes you have basic Rails knowledge from reading the Getting Started with Rails Guide.
15
16 == Command Line Basics ==
17
18 There are a few commands that are absolutely critical to your everyday usage of Rails. In the order of how much you'll probably use them are:
19
20 * console
21 * server
22 * rake
23 * generate
24 * rails
25
26 Let's create a simple Rails application to step through each of these commands in context.
27
28 === rails ===
29
30 The first thing we'll want to do is create a new Rails application by running the `rails` command after installing Rails.
31
32 NOTE: You know you need the rails gem installed by typing `gem install rails` first, right? Okay, okay, just making sure.
33
34 [source,shell]
35 ------------------------------------------------------
36 $ rails commandsapp
37
38 create
39 create app/controllers
40 create app/helpers
41 create app/models
42 ...
43 ...
44 create log/production.log
45 create log/development.log
46 create log/test.log
47 ------------------------------------------------------
48
49 Rails will set you up with what seems like a huge amount of stuff for such a tiny command! You've got the entire Rails directory structure now with all the code you need to run our simple application right out of the box.
50
51 NOTE: This output will seem very familiar when we get to the `generate` command. Creepy foreshadowing!
52
53 === server ===
54
55 Let's try it! The `server` command launches a small web server written in Ruby named WEBrick which was also installed when you installed Rails. You'll use this any time you want to view your work through a web browser.
56
57 NOTE: WEBrick isn't your only option for serving Rails. We'll get to that in a later section. [XXX: which section]
58
59 Here we'll flex our `server` command, which without any prodding of any kind will run our new shiny Rails app:
60
61 [source,shell]
62 ------------------------------------------------------
63 $ cd commandsapp
64 $ ./script/server
65 => Booting WEBrick...
66 => Rails 2.2.0 application started on http://0.0.0.0:3000
67 => Ctrl-C to shutdown server; call with --help for options
68 [2008-11-04 10:11:38] INFO WEBrick 1.3.1
69 [2008-11-04 10:11:38] INFO ruby 1.8.5 (2006-12-04) [i486-linux]
70 [2008-11-04 10:11:38] INFO WEBrick::HTTPServer#start: pid=18994 port=3000
71 ------------------------------------------------------
72
73 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 http://localhost:3000. I'll wait.
74
75 See? Cool! It doesn't do much yet, but we'll change that.
76
77 === generate ===
78
79 The `generate` command uses templates to create a whole lot of things. You can always find out what's available by running `generate` by itself. Let's do that:
80
81 [source,shell]
82 ------------------------------------------------------
83 $ ./script/generate
84 Usage: ./script/generate generator [options] [args]
85
86 ...
87 ...
88
89 Installed Generators
90 Builtin: controller, integration_test, mailer, migration, model, observer, performance_test, plugin, resource, scaffold, session_migration
91
92 ...
93 ...
94 ------------------------------------------------------
95
96 NOTE: You can install more generators through generator gems, portions of plugins you'll undoubtedly install, and you can even create your own!
97
98 Using generators will save you a large amount of time by writing *boilerplate code* for you -- necessary for the darn thing to work, but not necessary for you to spend time writing. That's what we have computers for, right?
99
100 Let's make our own controller with the controller generator. But what command should we use? Let's ask the generator:
101
102 NOTE: All Rails console utilities have help text. For commands that require a lot of input to run correctly, you can just try the command without any parameters (like `rails` or `./script/generate`). For others, you can try adding `--help` or `-h` to the end, as in `./script/server --help`.
103
104 [source,shell]
105 ------------------------------------------------------
106 $ ./script/generate controller
107 Usage: ./script/generate controller ControllerName [options]
108
109 ...
110 ...
111
112 Example:
113 `./script/generate controller CreditCard open debit credit close`
114
115 Credit card controller with URLs like /credit_card/debit.
116 Controller: app/controllers/credit_card_controller.rb
117 Views: app/views/credit_card/debit.html.erb [...]
118 Helper: app/helpers/credit_card_helper.rb
119 Test: test/functional/credit_card_controller_test.rb
120
121 Modules Example:
122 `./script/generate controller 'admin/credit_card' suspend late_fee`
123
124 Credit card admin controller with URLs /admin/credit_card/suspend.
125 Controller: app/controllers/admin/credit_card_controller.rb
126 Views: app/views/admin/credit_card/debit.html.erb [...]
127 Helper: app/helpers/admin/credit_card_helper.rb
128 Test: test/functional/admin/credit_card_controller_test.rb
129 ------------------------------------------------------
130
131 Ah, the controller generator is expecting parameters in the form of `generate controller ControllerName action1 action2`. Let's make a `Greetings` controller with an action of *hello*, which will say something nice to us.
132
133 [source,shell]
134 ------------------------------------------------------
135 $ ./script/generate controller Greeting hello
136 exists app/controllers/
137 exists app/helpers/
138 create app/views/greeting
139 exists test/functional/
140 create app/controllers/greetings_controller.rb
141 create test/functional/greetings_controller_test.rb
142 create app/helpers/greetings_helper.rb
143 create app/views/greetings/hello.html.erb
144 ------------------------------------------------------
145
146 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. All from one command!
147