How do you start a new Ruby project? There's no one answer to this question, but for many it involves firing up a text editor and saving some Ruby code into a file. But with Rails' application directory hierarchy, it's not that easy. Expecting users to remember to make all the directories and putting the files in the right place is expecting way too much, and Rails is about rapid development, doing things manually isn't rapid.
If you've installed Rails correctly, you have a rails command line program. This is where you'll generate new projects, among other things. To ensure that it's installed, run the rails --version command. You should see output that looks something like this.
~$ rails --version Rails 3.2.1
Running the rails new project_name command generates a new Rails project (replace project_name with something like my_awesome_blog for slightly better results). This will create a new directory in the current directory with that name, and all of the Rails project files inside. If you want this created in a specific directory, you can give it a full or relative path name instead, like rails new ~/rails_projects/new_blog.
When you run this command, you'll see all sorts of things scroll by. It will create all sorts of new files and directories and run a bundle install. Once it's finished (the bundle step can take a few moments), fire up your text editor. Now is the time when, if you haven't read the previous article on choosing a text editor, you really wish you did. At the very least at this stage you'll need a text editor with a file hierarchy display. So open up your text editor in this directory and take a gander at the files and directories.
Starting a Rails Project
When developing your application, you don't need to deploy it to any servers to see your results. By running the rails server command, a web server will be started (often on port 3000, but check the output of the command first). This server is not always the fastest (if you haven't installed any other servers, WEBrick will be used which, while it works fine, is quite slow) but it's fine for a single developer or a few people poking at it. This is not how you'll be deploying your applications though, this is only for development.
At any rate, if you run this command and then point your web browser to http://localhost:3000/, you should see a Rails welcome page. This is not actually part of your application, this is the public/index.html file. The first step in making an application is usually to generate a new projects, generate a new controller and then delete this file. However, we'll leave it for now.
Now that you've visited your own web application, something should have shown up in the logs, no? Go ahead and open up log/development.log either in a text editor or cat log/development.log from the command line. Here you can see every request that comes into the application. Lines like Started GET "/some/url" will indicate that it started to process an HTTP request. The following lines will say how it was handled and give you information about SQL queries issued. This file will be very useful when coding web applications, always remember to check it when something goes wrong.
It's probably not a good idea to leave this open in a text editor. Instead, open up a new terminal window, change to your Rails project directory and enter the command tail -f log/development.log. On Linux and OS X, this will print the last few lines of the development log and then wait for new lines to appear. As new lines are written to the file, the tail command will print them to the terminal window. Keep this window around and you'll just need to switch to it to check your log for errors.
You have a shiny new Rails project, but what next? Right now it doesn't do anything useful, so in the next article we'll look at getting rid of that index.html page and generate a new controller and view to serve up some pages. No model yet, it won't be a full web application, just a new test page of our own design. After that we'll dig into the git workflow, test-driven development and we'll finally be able to make our first model, hook it up to a controller and see what it looks like using the view. I know it sounds like a lot of work for a simple project, but using a solid git workflow and using test-driven development methodologies will save you a lot of time later. Stick with it, you can't build a house until you have a solid foundation, after all.