Rails是一款Ruby的web开发框架。因其及其灵活,强大的特点,开始慢慢的被人们接受了。Rails框架也是一款遵循MVC模式。
如图所示,Rails的工作原理是通过浏览器发送请求,交由控制器处理。控制器根据请求类型,选择相应的动作进行处理,动作可以选择调用具体的功能模式。将处理结果返回给视图进行处理。视图通过浏览器展示给用户。
我知道上面说的大概都是废话,哈哈,下面我们将以一个简单的例子来说一说Rials是怎么工作的。我们要开发Rials需要什么工具吗?答案当然是的,需要有Ruby和Rails。具体Rails的安装我这里不再赘述。我们直接进入主题把。
创建Rails的第一步就是需要创建一个工程。我们打开CMD窗口。切换到我们的工作空间,在CMD窗口下敲入命令
Rails FirstRuby
我们创建的Rails的工程名为FirstRuby。看看上面的流程图。我们下面该干嘛呢?控制器?恭喜你,你答对了。我们该怎么创建控制器呢?答案是很简单的,我们需要切换到刚刚创建的rails工程的目录当中。
>cd FirstRuby
FirstRuby>ruby script/generate controller Hello
我们切换到FirstRuby目录当中时,在命令行当中敲入上面的命令,我们就很简单的创建了一个控制器,这个控制器的名称就叫做hello。我们切换到工程目录下的,首先我们切换进入刚刚创建的Rails工程目录当中app/controllers下。我们会看到什么呢?你一定会看见hello_controller.rb这样一个文件。我们打开这个文件看看。
class HelloController < ApplicationController
end
这是一个ruby类,这个类继承了ApplicationController。ApplicationController是何许人也?后面的博客当中我们会详细说明的。
创建了控制器,我们下面要干嘛呢?继续看看流程图。恩,正如你所看到的,我们需要创建一个动作。动作怎么创建呢?答案也是很简单的,我们在刚刚打开的Ruby文件当中敲入以下代码
class HelloController < ApplicationController
def To
@model = Model.new("foolfish")
@message = @model.say
end
我们定义了一个To方法,To方法实例化了一个Model方法,并将Model类当中的say方法的返回值赋值给message。这里的To方法其实也就是一个动作。Rails当中的动作负责具体的方法。而To方法当中实例化的Model类再这里充当的是一个模式。对,就是你在流程图当中看见的。我们来看一看模式里面的代码吧
class Model
def initialize(mess)
@message = mess.capitalize;
end
def say
return "Hello #{message}"
end
end
该模式类接受一个参数,调用该模式类的say方法会返回hello + 参数的形式。模式层的代码存放再rails工程app/models目录当中。我们有了模式,控制器和动作,我们还差什么呢?答案是视图。
我们在app/views/hello目录下可以创建视图,rails当中视图文件是一rhtml格式保存的。我们创建To.rhtml文件。这里视图的文件名和动作名是一样的,这样动作可以方便的找到视图。免去了很多配置xml格式文件的繁琐问题。我们来看下视图文件的代码把。
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
<title>test</title>
</head>
<body>
<h1><%=@message%></h1>
</body>
</html>
一切具备了,只欠东风。下面我们只需要启动我们的服务器即可了。rails自带了一个webrick服务器。可以很方便的发布我们编写的Rails系统,我们只需要在DOS窗口下输入如下命令
FirstRuby>ruby script/server
这样我们就可以很容易的的启动服务器了。这时候我们只需要在浏览器当中输入URL就可以访问我们的程序了。这个地址就是
http://127.0.0.1:3000/Hello/To
Hello代表的是控制器的名称,To代表的是请求的动作名称。点击回车,我们就可以看见一个标题为Hello foolfish的界面。这样我们的第一个rails程序就可以发布了。
分享到:
相关推荐
Based on the bestselling first and second editions, Beginning Ruby, 3rd Edition is a leading guide to learn Ruby from the ground up. The new edition of this book provides the same excellent ...
On the other hand, Beginning Ruby on Rails is all about programming, so if you have programming expe- rience, that will be helpful. If you want to know how to create Ruby on Rails online applications,...
Beginning Ruby is a thoroughly contemporary guide for every type of reader who wants to learn Ruby, from novice programmers to web developers to Ruby newcomers. It starts by explaining the principles ...
Beginning Ruby starts by explaining the principles behind object-oriented programming and within a few chapters builds toward creating a full Ruby application. By the end of the book, in addition to ...
Beginning Ruby on rails 源代码
Wrox - Beginning Ruby on Rails 不错的资源
Based on the bestselling first and second editions, Beginning Ruby, Third Edition is a leading guide to learn Ruby from the ground up. The new edition of this book provides the same excellent ...
Beginning Ruby From Novice to Professional 2nd Edition.pdf
Apress - Beginning Ruby - From Novice to Professional (2007) ruby 2007年新书 pdf格式
Based on the bestselling first and second editions, Beginning Ruby, Third Edition is a leading guide to learn Ruby from the ground up. The new edition of this book provides the same excellent ...
Beginning_Ruby_From_Novice_to_Professional.pdf
Apress源代码该存储库随附Peter Cooper撰写的 (Apress,2016年)。 使用绿色按钮将文件下载为zip格式,或使用Git将存储库克隆到您的计算机上。发行版v1.0版对应于已出版书籍中的代码,没有更正或更新。...
Beginning.Ruby-From.Novice.to.Professional,2nd.Edition
In this comprehensive guide to Ruby optimization, you'll learn how to write faster Ruby code--but that's just the beginning. See exactly what makes Ruby and Rails code slow, and how to fix it. Alex ...
In this comprehensive guide to Ruby optimization, you’ll learn how to write faster Ruby code–but that’s just the beginning. See exactly what makes Ruby and Rails code slow, and how to fix it. Alex...
In this comprehensive guide to Ruby optimization, you’ll learn how to write faster Ruby code–but that’s just the beginning. See exactly what makes Ruby and Rails code slow, and how to fix it. Alex...