Ruby on Rails的Hello World

1. 根据「Mac上快速安装Ruby on Rails」一文中的4~6部建立一个名为demo的工程

2. 进入demo文件夹下,建立一个名为controller的控制器

bin/rails g controller welcome

3. 在config/routes.rb里插入一些控制设定

Rails::Application.routes.draw do

  get "welcome/say_hello" => "welcome#say"

  get "welcome" => "welcome#index"

  root :to => "welcome#index"

end

4. 编辑app/controllers/welcome_controller.rb

class WelcomeController < ApplicationController

  def say

  end

  def index

  end

end

5. 在app/views/welcome/目录下新建say.html.erb与index.html.erb

say.html.erb:

Hello, World!

<%= link_to "Home", root_path %>

index.html.erb:

Hola! It's <%= Time.now %>

<%= link_to 'Hello!', welcome_say_hello_path %>

6. 打开浏览器输入localhost:3000/demo即可查看结果。这个范例应该能够在两个页面之间互相跳转。

(整理至《Ruby on Rails 實戰聖經》)

你可能感兴趣的:(Ruby on Rails的Hello World)