Beginning Ruby

      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程序就可以发布了。

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Learn the principles behind object-oriented programming and within a few chapters create a fully functional Ruby application. You'll also gain a basic understanding of many ancillary technologies such as databases, XML, web frameworks, and networking - some of which are needed as part of a fully functioning Ruby application. 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 introduction to Ruby as the previous editions plus updates for the newest version of Ruby 2.3. This book can also be used as a textbook or companion to a textbook on beginning Ruby programming. The light and agile Ruby programming language remains a very popular open source scripting option for developers building today's web and even some enterprise applications. And, now, Ruby also has applications using the Raspberry Pi, popular among hobbyists and makers. Many former Java developers still use Ruby on Rails today, the most popular framework for building Ruby applications. What You'll Learn What are the fundamentals of Ruby and its object-oriented building blocks How to work with Ruby libraries, gems, and documentation How to work with files and databases How to write and deploy Ruby applications What are the various Ruby web frameworks and how to use them How to do network programming with Ruby and more Who This Book Is For This book is for beginning programmers, programmers new to Ruby, and web developers interested in learning and knowing the foundations of the Ruby programming language. Table of Contents Part 1: Foundations and Scaffolding Chapter 1: Let’s Get It Started: Installing Ruby Chapter 2: Programming == Joy: A Whistle-Stop Tour of Ruby and Object Orientation Chapter 3: Ruby’s Building Blocks: Data, Expressions, and Flow Control Chapter 4: Developing Your First Ruby Application Chapter 5: The Ruby Ecosystem Part 2: The Core of Ruby Chapter 6: Classes, Objects, and Modules Chapter 7: Projects and Libraries Chapter 8: Documentation, Error Handling, Debugging, and Testing Chapter 9: Files and Databases Chapter 10: Distributing Ruby Code and Libraries Chapter 11: Advanced Ruby Features Chapter 12: Tying It Together: Developing a Larger Ruby Application Part 3: Ruby Online Chapter 13: Two Web Application Approaches: Rails and Sinatra Chapter 14: Ruby and the Internet Chapter 15: Networking and Sockets Chapter 16: Useful Ruby Libraries Appendix A: Ruby Primer and Review for Developers Appendix B: Useful Resources

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值