Ruby实践—HelloWorld

开发环境

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 development: adapter: mysql database: test username: root password: root 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 def hello end end

5、创建hello.rhtml.erb

在app/views/say 目录下创建hello.rhtml.erb

修改其中的内容如下:

<html> <head> <title>Hello, Rails!</title> </head> <body> <h1>Hello from Rails!</h1> </body> </html>

6、修改routes.rb

修改config/routes.rb,设置新的映射规则

#路由设置,/say/Hello是地址设置;controller是对应的controllers目录下的类,action指controller中定义的方法,区别大小写 map.connect '/say/Hello',:controller=>"say",:action=>"hello" map.connect ':controller/:action/:id' map.connect ':controller/:action/:id.:format'

启动服务Rails服务器,运行 http://localhost:3000/say/Hello (注意匹配大小写)

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值