ruby on rails 自学笔记 (2)入门篇 第一个页面

昨天安装完成rails 之后,早上来了动手实现了第一个页面,过程如下,创建controller,创建view页面,修改路由 ,首先来创建第一个controller,在rails命令窗口,进入项目目录,然后运行:rails  generate controller say,                                                         

 可以看到创建的文件 views 生成了say文件夹用来存放saycontroller对应的页面。生成了单元测试文件。

修改config\routes.rb 文件 添加“match ':controller/:action'”

在saycontroller里添加一个action方法,hello,

class SayController < ApplicationController
  def hello
    end

end 

然后在views\say文件夹下创建action对应的页面,hello.html.erb,里面是要展现的html代码 

<html>
<body>
hello world
</body>

</html> 

启动服务:ruby script\rails  server 

可以看到启动的进程的Id和端口号3000,在浏览器中输入localhost:3000/say/hello,页面显示hello world。

原文地址:https://www.cnblogs.com/dlz1123/p/2058044.html