A guide to creating the canvas of your first Ruby on Rails app.
If you haven't installed Rails yet, head over to Jeff Cohen fantastic Guide to Installing Rails, and come back when you're done.
Congratulations, you've successfully installed Rails! Pat yourself on the back.
Now create a "Projects" folder in your home directory. This folder will hold all your projects. You can organise it as you wish, but make sure each application lives in its own folder. This is important because you will use Git, which works on a per-directory ("repo" in Git-speak) basis.
Ok, now let's get started with creating that brand new app. Let's call it AirportStatusApp. The vision: give travellers useful data, such as delays and weather.
Let's get started
Open your terminal window. At open, you are in your home directory.
cd command to navigate to your Projects directory:
Ok, you are presently in your Projects directory.
Now to create a new rails app, just type in the following command in your terminal:
rails new AirportStatusApp
That command will spit out dozens of lines, all starting with "create". This is Rails creating all the files and folders you need to run it. All of which are in a folder Rails created for you, which bears the name we specified right after the
rails new command. In our case:
Now navigate to your newly minted AirportStatusApp folder by typing:
Run your rails server:
Your terminal should display something like this:
=> Booting WEBrick => Rails 4.0.0 application starting in development on http://0.0.0.0:3000 => Run `rails server -h` for more startup options => Ctrl-C to shutdown server [2013-10-25 14:22:33] INFO WEBrick 1.3.1 [2013-10-25 14:22:33] INFO ruby 2.0.0 (2013-06-27) [x86_64-darwin12.3.0] [2013-10-25 14:22:33] INFO WEBrick::HTTPServer#start: pid=1698 port=3000
I won't explain what all this means line by line. Because I don't have a clue of what they all mean.
What I do know however is this : Rails has started a new web server for your app, and it currently lives at
http://0.0.0.0:3000. 0.0.0.0 is another name for localhost. That's your computer. And 3000 is the port it's using. That's right, your rails app is now running on your machine.
To access it, copy
http://0.0.0.0:3000 in your browser's address bar and marvel :
Rails' getting started instructions makes things look quick and easy. But it ain't. These instructions are just too high altitude for us noobs. We're not flying yet. We're only barely crawling. So we'll just ignore all these instructions for now.
Now, let's go see what files Rails has created for us. To do so, head back to your terminal window's shell prompt and use your favorite code editor to open that folder. With Sublime Text:
"." is computer-speak for root. So the "subl ." command always opens the entire tree of your current working directory, starting from the root : directories, subdirectories and files.
I highly recommend Sublime Text 2. It's smart syntax highlighting and keyboard shortcuts will make your coder's life easier. The best 25 dollars you'll ever spend. Again, Jeff has a superb guide to setting up Sublime Text.
Recap for setting up a new Rails application
- Install Rails on your computer
rails new [NameOfYourApp]
Now that your Rails app is up, the next step is to create our first web page. To do that, we have to set up our routes.