開發環境
OS:WindowsXP
Ruby: Ruby1.9.1
Rails: Rails2.3.5
IDE: RubyMine2.0.1
1、建立Rails工程
2、修改 /config/database.yml
自動建立的工程中預設資料庫串連的是sqlite,如果沒有安裝此資料庫,需要修改該配置(本例中使用的是mysql)
# Mysql Version 5.1.46<br />development:<br /> adapter: mysql<br /> database: test<br /> username: root<br /> password: root<br /> host: localhost
3、建立Controller
在app/controller中建立say_controller.rb
建立完成後,在控制台資訊中將顯示此過程建立的一系列檔案
C:/Ruby19/bin/ruby.exe -e STDOUT.sync=true;STDERR.sync=true;load($0=ARGV.shift) E:/Ruby/HelloWorld/script/generate controller -s say
exists app/controllers/
exists app/helpers/
create app/views/say
exists test/functional/
create test/unit/helpers/
create app/controllers/say_controller.rb
create test/functional/say_controller_test.rb
create app/helpers/say_helper.rb
create test/unit/helpers/say_helper_test.rb
Process finished with exit code 0
4、修改say_controller.rb
將內容修改如下:
class SayController < ApplicationController<br /> def hello<br /> end<br />end
5、建立hello.rhtml.erb
在app/views/say 目錄下建立hello.rhtml.erb
修改其中的內容如下:
<html><br /><head><br /><title>Hello, Rails!</title><br /></head></p><p><body><br /><h1>Hello from Rails!</h1><br /></body><br /></html>
6、修改routes.rb
修改config/routes.rb,設定新的映射規則
#路由設定,/say/Hello是地址設定;controller是對應的controllers目錄下的類,action指controller中定義的方法,區別大小寫<br /> map.connect '/say/Hello',:controller=>"say",:action=>"hello"<br /> map.connect ':controller/:action/:id'<br /> map.connect ':controller/:action/:id.:format'
啟動服務Rails伺服器,運行 http://localhost:3000/say/Hello (注意匹配大小寫)