27.02.2013 Views

Rails%203%20In%20Action

Rails%203%20In%20Action

Rails%203%20In%20Action

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

6 CHAPTER 1 Ruby on Rails, the framework<br />

Next, you need to install the rails gem. The following command installs both<br />

Rails and its dependencies. If you’re using the Rails installer you will not need to run<br />

this command because Rails will already be installed:<br />

gem install rails -v 3.1.0<br />

1.2.2 Generating an application<br />

With Rails now installed, to generate an application, you run the rails command and<br />

pass it the new argument and the name of the application you want to generate:<br />

things_i_bought. When you run this command, it creates a new directory called<br />

things_i_bought, which is where all your application’s code will go. You can call your<br />

application anything you wish, but it can’t be given the same name as a reserved word<br />

in Rails. For example, you wouldn’t call your application rails because it defines a Rails<br />

constant, which is internal to Rails, and the two constants would clash.<br />

The application that you’re going to generate will be able to record purchases you<br />

have made. You can generate it using this command:<br />

rails new things_i_bought<br />

The output from this command may seem a bit overwhelming at first, but rest assured:<br />

it’s for your own good. All of the directories and files generated here provide the<br />

building blocks for your application, and you’ll get to know each of them as we progress.<br />

For now, let’s get rolling and learn by doing, which is the best way of learning.<br />

1.2.3 Starting the application<br />

To get the server running, you must first change into the newly created application’s<br />

directory and then run these commands to start the application server:<br />

cd things_i_bought<br />

bundle install<br />

rails server<br />

The bundle install command installs all the gems required for your application.<br />

This is explained in further detail in chapter 3.<br />

This starts a web server on your local address on port 3000 using a Ruby standard<br />

library web server known as WEBrick. It will say it’s “starting in development on<br />

http://0.0.0.0:3000,” which indicates to you that the server will be available on port<br />

3000 on all network interfaces of this machine. 8 To connect to this server, go to<br />

http://localhost:3000 in your favorite browser. You’ll see the “Welcome aboard”<br />

page, which is so famous in Rails (see figure 1.1).<br />

If you click About Your Application’s Environment, you’ll find your Ruby, Ruby-<br />

Gems, Ruby on Rails, and Rack versions and other environmental data. One of the<br />

things to note here is that the output for Environment is Development. Rails provides<br />

8 This is what the 0.0.0.0 address represents. It is not an actual address, so to speak, and so localhost or<br />

127.0.0.1 should be used.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!