《Agile Web Development with Rails》读书笔记（二）
After the Rails environment has been established, I write a simple application to verify we have got Rails snugly installed on my machine.
I. Create a new project
Rails application is a bunch of Ruby source code, but does a lot of magic behind the curtain to minimum the explicit configuration.
I’ll use a new command-line tool rails to create my first project.
To create the first Rails application, pop open a shell window, navigate to a place in the file system where I want to create the application directory structure.
This command creates a bunch of files and subdirectories, you can cd demo to have a look at it.
Two directories will be used in this example:
app: the kernel of MVC, put application code files here.
script: contain some useful utility scripts.
In order to see the result of create application, I start the stand-alone develop web server:
note: this command will hang up the command shell, so open another shell window, change to the application directory to run the above command.
The service start on port 3000. I access the application using the URL
the work of “create new application” is finished when I see the “welcome aboard” page in the browser.
Now, let’s write some content for this application.
II. Hello Rails!
Before I write some content for the application, first I introduce some concept.
The Architecture of Rails:
Rails accept a request from browser, decodes the request to find the controller, and call the action method in that controller. The controller then invoke a particular view to display the result to the user.
Rails and Request URLs
Like other web application, Rails application appears to its users to be associate with a URL.
I’ll take a URL as an example to explain it:
Rails uses the path to determine the name of the controller to use and the name of the action to invoke in that controller.
My First Action
Ok, now let’s write some content for our application.
Followed the discussion in the previous section, the hello action means create a method in the class SayController.
The controller’s job is to set up things so that the view knows what to display.
class SayController > ApplicationController
<h1>Hello from Rails!</h1>
Save these two files, and refresh the browser, then you should see my friendly greeting.