- 在 DOS 命令行窗口運行 rails student(本程序的工作目錄是 C:\ )
C:\>rails student
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
C:\>
你可以看到,rails 已經(jīng)為你生成了 student 應(yīng)用程序的完整目錄結(jié)構(gòu)。
Rails 不僅僅是一個運行時網(wǎng)絡(luò)應(yīng)用框架,它本身包含了豐富的腳本來幫你完成程序基本骨架的工作。
運行 rails student 后, rails 將生成 student 應(yīng)用程序的默認目錄結(jié)構(gòu)和初始化文件。
Rails 開發(fā)的方式是: 由 rails 來生成默認的應(yīng)用程序骨架,你所做的工作就是在默認目錄結(jié)構(gòu)中編輯文件增加應(yīng)用程序邏輯就可以了。
緊接著測試一下空的應(yīng)用程序,看看 rails 默認給我們完成了什么工作。
本程序的工作目錄是 D:\railsdoc>, 上面 rails 已經(jīng)給我們生成了student目錄,進入 student 目錄。
運行 ruby script\server 。 這條命令是運行 script 目錄下的 server 命令來啟動 webrick 服務(wù)器。
啟動 webrick 服務(wù)器如下:
C:\student>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-04-18 17:02:32] INFO WEBrick 1.3.1
[2007-04-18 17:02:32] INFO ruby 1.8.6 (2007-03-13) [i386-mswin32]
[2007-04-18 17:02:32] INFO WEBrick::HTTPServer#start: pid=3692 port=3000
打開瀏覽器 ,輸入網(wǎng)址http://127.0.0.1:3000/ 你會看到類似的網(wǎng)頁。
暫時不要關(guān)閉這個窗口。
Webrick 簡介:
Ruby 默認已經(jīng)包含了豐富的軟件,其中 webrick http 服務(wù)器就是其中之一。 這個程序包被用作 rubygem 的默認文檔服務(wù)器。這是一個非常簡單的純 ruby 編寫的服務(wù)器。如果你掌握了 webrick 你可以用幾行代碼來運行一個 web 服務(wù)器。
打開編輯器,編寫以下腳本
require "webrick"
httpd = WEBrick::HTTPServer.new(
:DocumentRoot =>Dir::pwd + "/ruby",
:Port => 80
)
trap(:INT){ httpd.shutdown }
httpd.start
然后保存到 c:\server.rb 下,雙擊 server.rb , 一個最簡單的 http server 就運行了,它將 c:\ruby 目錄作為服務(wù)器文檔根目錄。
C:\student>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-04-18 17:02:32] INFO WEBrick 1.3.1
[2007-04-18 17:02:32] INFO ruby 1.8.6 (2007-03-13) [i386-mswin32]
[2007-04-18 17:02:32] INFO WEBrick::HTTPServer#start: pid=3692 port=3000
127.0.0.1 - - [18/Apr/2007:17:09:18 中國標準時間] "GET / HTTP/1.1" 200 7552
- -> /
127.0.0.1 - - [18/Apr/2007:17:09:19 中國標準時間] "GET /javascripts/prototype.js HTTP/1.1" 200 71260 - http://127.0.0.1:3000/ -> /javascripts/prototype.js
127.0.0.1 - - [18/Apr/2007:17:09:19 中國標準時間] "GET /javascripts/effects.js HTTP/1.1" 200 38200
http://127.0.0.1:3000/ -> /javascripts/effects.js
127.0.0.1 - - [18/Apr/2007:17:09:19 中國標準時間] "GET /images/rails.png HTTP/1.1" 200 1787
http://127.0.0.1:3000/ -> /images/rails.png
http://127.0.0.1/
在瀏覽器窗口打開
你將會看到 c:/ruby 目錄的內(nèi)容,我們的 8 行 ruby 代碼就生成了一個簡單 http 服務(wù)器。你不光可以定制文檔根目錄,你還可以象編寫 java servlet 那樣,為 webrick 編寫 ruby servlet 代碼。具體詳細信息參看 www.webrick.org