<divclass="paragraph"><p>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:</p></div>
<divclass="paragraph"><p>The first thing we’ll want to do is create a new Rails application by running the <tt>rails</tt> command after installing Rails.</p></div>
<divclass="paragraph"><p>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.</p></div>
<divclass="paragraph"><p>Let’s try it! The <tt>server</tt> command launches a small web server named WEBrick which comes bundled with Ruby. You’ll use this any time you want to view your work through a web browser.</p></div>
<divclass="paragraph"><p>Here we’ll flex our <tt>server</tt> command, which without any prodding of any kind will run our new shiny Rails app:</p></div>
<divclass="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 <ahref="http://localhost:3000">http://localhost:3000</a>. I’ll wait.</p></div>
<divclass="paragraph"><p>See? Cool! It doesn’t do much yet, but we’ll change that.</p></div>
<divclass="paragraph"><p>The <tt>generate</tt> command uses templates to create a whole lot of things. You can always find out what’s available by running <tt>generate</tt> by itself. Let’s do that:</p></div>
<tdclass="content">You can install more generators through generator gems, portions of plugins you’ll undoubtedly install, and you can even create your own!</td>
<divclass="paragraph"><p>Using generators will save you a large amount of time by writing <strong>boilerplate code</strong> 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?</p></div>
<divclass="paragraph"><p>Let’s make our own controller with the controller generator. But what command should we use? Let’s ask the generator:</p></div>
<tdclass="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>
<divclass="paragraph"><p>Ah, the controller generator is expecting parameters in the form of <tt>generate controller ControllerName action1 action2</tt>. Let’s make a <tt>Greetings</tt> controller with an action of <strong>hello</strong>, which will say something nice to us.</p></div>
<divclass="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>
<divclass="paragraph"><p>Let’s check out the controller and modify it a little (in <tt>app/controllers/greeting_controller.rb</tt>):</p></div>
<divclass="listingblock">
<divclass="content"><!-- Generator: GNU source-highlight 2.9
<spanstyle="color: #009900">@message</span><spanstyle="color: #990000">=</span><spanstyle="color: #FF0000">"Hello, how are you today? I am exuberant!"</span>
<divclass="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>
<divclass="listingblock">
<divclass="content"><!-- Generator: GNU source-highlight 2.9
<divclass="paragraph"><p>The URL will be <tt>http://localhost:3000/greetings/hello</tt>. I’ll wait for you to be suitably impressed.</p></div>
<divclass="admonitionblock">
<table><tr>
<tdclass="icon">
<imgsrc="./images/icons/note.png"alt="Note"/>
</td>
<tdclass="content">With a normal, plain-old Rails application, your URLs will generally follow the pattern of <ahref="http://(host)/(controller)/(action">http://(host)/(controller)/(action</a>), and a URL like <ahref="http://(host)/(controller">http://(host)/(controller</a>) will hit the <strong>index</strong> action of that controller.</td>
</tr></table>
</div>
<divclass="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>
<divclass="listingblock">
<divclass="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt>$ <spanstyle="color: #990000">.</span>/script/generate model
`<spanstyle="color: #990000">.</span>/script/generate model post title<spanstyle="color: #990000">:</span>string body<spanstyle="color: #990000">:</span>text published<spanstyle="color: #990000">:</span>boolean`
creates a Post model with a string title<spanstyle="color: #990000">,</span> text body<spanstyle="color: #990000">,</span> and published flag<spanstyle="color: #990000">.</span></tt></pre></div></div>
<divclass="paragraph"><p>But instead of generating a model directly (which we’ll be doing later), let’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>
<divclass="paragraph"><p>Let’s set up a simple resource called "HighScore" that will keep track of our highest score on video games we play.</p></div>
<divclass="listingblock">
<divclass="content"><!-- Generator: GNU source-highlight 2.9
<divclass="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>
<divclass="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’ll talk more about Rake in-depth in a little while.</p></div>
<divclass="admonitionblock">
<table><tr>
<tdclass="icon">
<imgsrc="./images/icons/note.png"alt="Note"/>
</td>
<tdclass="content">Hey. Install the sqlite3-ruby gem while you’re at it. <tt>gem install sqlite3-ruby</tt></td>
</tr></table>
</div>
<divclass="listingblock">
<divclass="content"><!-- Generator: GNU source-highlight 2.9
<tdclass="content">Let’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’ll make one in a moment.</td>
</tr></table>
</div>
<divclass="paragraph"><p>Let’s see the interface Rails created for us. ./script/server; <ahref="http://localhost:3000/high_scores">http://localhost:3000/high_scores</a></p></div>
<divclass="paragraph"><p>We can create new high scores (55,160 on Space Invaders!)</p></div>
<h3id="_console">1.4. console</h3>
<divclass="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’ve ever used it, you’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>
<h3id="_dbconsole">1.5. dbconsole</h3>
<divclass="paragraph"><p><tt>dbconsole</tt> figures out which database you’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>
<h3id="_plugin">1.6. plugin</h3>
<divclass="paragraph"><p>The <tt>plugin</tt> command simplifies plugin management; think a miniature version of the Gem utility. Let’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>
<divclass="paragraph"><p>Let’s say you’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’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>
<divclass="paragraph"><p>There is such a thing! The plugin we’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>
<divclass="listingblock">
<divclass="content"><!-- Generator: GNU source-highlight 2.9
<divclass="paragraph"><p>Think of <tt>destroy</tt> as the opposite of <tt>generate</tt>. It’ll figure out what generate did, and undo it. Believe you-me, the creation of this tutorial used this command many times!</p></div>
<divclass="listingblock">
<divclass="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt>$ <spanstyle="color: #990000">.</span>/script/generate model Oops
<divclass="paragraph"><p>Check it: Version numbers for Ruby, RubyGems, Rails, the Rails subcomponents, your application’s folder, the current Rails environment name, your app’s database adapter, and schema version! <tt>about</tt> is useful when you need to ask help, check if a security patch might affect you, or when you need some stats for an existing Rails installation.</p></div>
<divclass="listingblock">
<divclass="content"><!-- Generator: GNU source-highlight 2.9