Madison Rails
Command Line Cheat Sheet (Version #1)

Learning to use the Command Line

Once you’re up and running with a working Ruby on Rails installation, here are a few other command line “scripts” to keep handy.

Start Locahost

You may remember this from the other wiki page about installing RoR ( RoR Installation Cheat Sheet ) so here’s a quick recap at how to start your localhost server. You may have your Rails app running somewhere else, this is just an example.

At the C:\> type:
cd rails_projects\NameOfMyApp
... hit enter, then at the C:\rails_projects\NameOfMyApp> type …
ruby script/server

... wait a second or two and you should see the server start spitting out a bunch of code in the Terminal window. This is good, now leave this window alone while you work on your new RoR app.

Navigating on the Command Line

To change directories, you obviously need to use “cd” but here’s how to look cool:
  • To move deeper into the document tree, type: cd and the name of your folder (i.e.: cd rails_projects). If you’re smart you’ll remember the path to your app (or any other place you want to go) and type it all into once as in “cd folder-1\folder-2\folder-3”
  • To go back to the root of your hard drive, type: “cd\”
  • To back out of a directory, type: “cd..” (no quotes)
  • Remember to keep those slashes going to correct direction. ”\” is used to separate directories and ”/” is used to separate scripts from methods
  • The command line is nice in that it remembers places you’ve been or scripts you’ve run, just use the up and down arrow cursor keys.
  • The command line can also guess where you’re trying to go. Let’s say I want to go to the “NameOfMyApp” folder within my “rails_projects” folder. All I need to do is type the first few letters of a dir name and hit tab:
C:\>cd ra (tab)
... which gives me:
C:\>cd rails_projects
... then I type the dir separator and the first few letters of my next dir:
/Na (tab)
... which now gets me to:
C:\>cd rails_projects\NameOfMyApp
... hit enter and you’re now in the NameOfMyApp folder. Wheeeeeee!

To build out your database (initial creation):

Ruby needs a database to do the cool stuff it does, but don’t worry, at first you will not need to know MySQL. Ruby writes MySQL for you when the following is typed:

ruby script/generate model "modelName"

Be sure to go into PHPMyAdmin and double check that Ruby did this correctly. Go to http://localhost/phpmyadmin

Edit database migration (build db):

Edit DB Migation means that you want to open your migration in a text editor and edit it.

db/migrate/modelName.rb

Update your database:

rake db:migrate

Build your controllers and related views:

Create a controller with views attached, type this:

ruby script/generate controller "controllerName" "viewName1" "viewName2"

Build out your scaffolding:

ruby script/generate scaffold "modelName" "controllerName"

For more info, Google it.