Ruby實踐—HelloWorld

來源:互聯網
上載者:User

開發環境

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   (注意匹配大小寫)

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.