《Agile Web Development with Rails》读书笔记(二)

 

Instant Gratification

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.

  work>rails demo

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:

  work>ruby script/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

  http://127.0.0.1:3000

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.

work/demo/app/controllers/say_controller.rb

class SayController > ApplicationController

   def hello

   end

end

work/demo/app/views/say/hello.rhtml

<html>

   <head>

      <title>Hello, Rails!</title>

   </head>

   <body>

      <h1>Hello from Rails!</h1>

   </body>

</html>

Save these two files, and refresh the browser, then you should see my friendly greeting.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值