Ruby on Rails研究之二:简单入门

  安装完成以后,下面要写个Hello world来玩玩了。

一、建立应用
  在windows的cmd命令行窗口进入d:/ror目录,输入命令rails test建立一个应用,其

中test是你希望的应用名称。
屏幕显示如下:
D:/ROR>rails test
      create
      create  app/controllers
      create  app/helpers
      create  app/models
      create  app/views/layouts
      create  config/environments
      create  components
      create  db
      create  doc
      create  lib
      create  lib/tasks
      create  log
      create  public/images
      create  public/javascripts
      create  public/stylesheets
      create  script/performance
      create  script/process
      create  test/fixtures
      create  test/functional
      create  test/integration
      create  test/mocks/development
      create  test/mocks/test
      create  test/unit
      create  vendor
      create  vendor/plugins
      create  tmp/sessions
      create  tmp/sockets
      create  tmp/cache
      create  tmp/pids
      create  Rakefile
      create  README
      create  app/controllers/application.rb
      create  app/helpers/application_helper.rb
      create  test/test_helper.rb
      create  config/database.yml
      create  config/routes.rb
      create  public/.htaccess
      create  config/boot.rb
      create  config/environment.rb
      create  config/environments/production.rb
      create  config/environments/development.rb
      create  config/environments/test.rb
      create  script/about
      create  script/breakpointer
      create  script/console
      create  script/destroy
      create  script/generate
      create  script/performance/benchmarker
      create  script/performance/profiler
      create  script/process/reaper
      create  script/process/spawner
      create  script/process/inspector
      create  script/runner
      create  script/server
      create  script/plugin
      create  public/dispatch.rb
      create  public/dispatch.cgi
      create  public/dispatch.fcgi
      create  public/404.html
      create  public/500.html
      create  public/index.html
      create  public/favicon.ico
      create  public/robots.txt
      create  public/images/rails.png
      create  public/javascripts/prototype.js
      create  public/javascripts/effects.js
      create  public/javascripts/dragdrop.js
      create  public/javascripts/controls.js
      create  public/javascripts/application.js
      create  doc/README_FOR_APP
      create  log/server.log
      create  log/production.log
      create  log/development.log
      create  log/test.log

可以看到rails为这个应用建立了一些目录和文件。进入d:/ror/test目录,输入命令

ruby script/server启动应用。ruby的速度比较慢,耐心等待,屏幕显示如下:
D:/ROR>cd test
D:/ROR/test>ruby script/server
=> Booting WEBrick...
=> Rails application started on http://0.0.0.0:3000
=> Ctrl-C to shutdown server; call with --help for options
[2007-03-27 14:05:03] INFO  WEBrick 1.3.1
[2007-03-27 14:05:03] INFO  ruby 1.8.5 (2006-12-25) [i386-mswin32]
[2007-03-27 14:05:03] INFO  WEBrick::HTTPServer#start: pid=3228 port=3000
这样应用就启动完成了,打开浏览器,输入http://127.0.0.1:3000就可以看到rails的

欢迎页面了。需要注意的是如果照着提示输入http://0.0.0.0:3000,是看不到的,提

示“找不到服务器”

这里还有一个要注意的问题,就是一定要在d:/ror/test目录输入命令启动应用,我一

开始为了省事,直接在d:/ror目录输入ruby test/script/server启动应用,应用可以

照常启动,但是在欢迎页面上点击“About your application's enviroment”链接会

出现显示错误
“`/rails/info/properties' not found. ”,在控制台会显示“#<ArgumentError:

Anonymous modules have no name to be referenced by>”,我浪费了我半天的时间

也没找到这个文件。结果是启动的目录不正确造成的,看来ruby的错误提示有待改善,

还有就是一步一步慢慢来,不要自作聪明。

顺便提一下,在控制台用CTRL+C可以停止应用。

二、建立hello world
  在d:/ror/test目录输入ruby script/generate controller hello建立一个控制器,

屏幕显示如下:
D:/ROR/test>ruby script/generate controller hello
      exists  app/controllers/
      exists  app/helpers/
      create  app/views/hello
      exists  test/functional/
      create  app/controllers/hello_controller.rb
      create  test/functional/hello_controller_test.rb
      create  app/helpers/hello_helper.rb
修改相应文件d:/ror/test/app/controllers/hello_controller.rb为
class HelloController < ApplicationController
  def world
  end
end
意义是定一个名为world的Action
然后建立相应模板文件d:/ror/test/app/views/hello/world.rhtml,文件内容:
<html>
 <head>
  <title>Hello World!</title>
 </head>
 <body>
  <h1>Hello World from Rails!</h1>
 </body>
</html>
rhtml文件是ERb(Embedded Ruby)文件格式. 在调用时Rails将解释里面的Ruby语句.当

然这个例子里面还没有ruby语句。在IE地址栏输入

http://127.0.0.1:3000/hello/world就可以看到这个网页了。

三、加入动态内容
修改文件d:/ror/test/app/controllers/hello_controller.rb为
class HelloController < ApplicationController
  def world
    @time = Time.now
  end
end
这里定义了一个名为@time的变量
然后修改相应模板文件d:/ror/test/app/views/hello/world.rhtml
<html>
 <head>
  <title>Hello World!</title>
 </head>
 <body>
  <h1>Hello World from Rails!</h1>
  <p>
   It is now <%= @time %>
  </p>
 </body>
</html>
这里引用了@time变量,包含在<% %>里面的是ruby语句,调用时rails将解释它。
在IE地址栏输入http://127.0.0.1:3000/hello/world就可以看到网页上增加了时间显示。
顺便提一下,rails使用的是自带的WEBRick服务器,他会定期检测文件修改,所以每次修改的时候不用重新启动,非常的方便。

四、链接其他Action
修改文件d:/ror/test/app/controllers/hello_controller.rb为
class HelloController < ApplicationController
  def world
    @time = Time.now
  end
  def world2
  end
end
这里增加了一个world2的Action
然后建立相应模板文件d:/ror/test/app/views/hello/world2.rhtml,文件内容:
<html>
 <head>
  <title>world2!</title>
 </head>
 <body>
  <h1>This is world2</h1>
  <p>
   Go to <%= link_to "hello world", :action => "world" %>
  </p>
 </body>
</html>
在IE地址栏输入http://127.0.0.1:3000/hello/world2就可以看到这个新的网页了。里面有一个通向world的链接。

 

 

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值