ROR学习系列1-第一个ROR程序
本文即以下系列文章参考了Steven Holzner的 Beginning Ruby on Rails.Welcome to Ruby on Rails.
对于Ruby,我们很容易的从网上得到它,在windows下,它是一个exe文件,直接安装就行了,安装Rails也很简单,在控制台输入:gem install rails --include-dependencies.等一段时间就可以完成安装了。你也可以利用集成开发环境,如netbeans进行开发,不用安装上面两个,但是对于初学者,还是建议安装一下为好,了解了解包的结构。当然ror是跨平台的,它还可以在更多的系统上运行,这里就不说了。
安装好平台之后,下面开始第一个ROR的例子。
进入dos下面,新建一个目录:myruby
d:\>md myruby
进入到这个目录下面
d:\>cd myruby
新建一个Rails工程
d:\myruby>rails hello
下面是创建的工程:
这里是具体的演示:
D:\>md myruby
D:\>cd myruby
D:\myruby>rails hello
create
createapp/controllers
createapp/helpers
createapp/models
createapp/views/layouts
createconfig/environments
createconfig/initializers
createdb
createdoc
createlib
createlib/tasks
createlog
createpublic/images
createpublic/javascripts
createpublic/stylesheets
createscript/performance
createscript/process
createtest/fixtures
createtest/functional
createtest/integration
createtest/mocks/development
createtest/mocks/test
createtest/unit
createvendor
createvendor/plugins
createtmp/sessions
createtmp/sockets
createtmp/cache
createtmp/pids
createRakefile
createREADME
createapp/controllers/application.rb
createapp/helpers/application_helper.rb
createtest/test_helper.rb
createconfig/database.yml
createconfig/routes.rb
createpublic/.htaccess
createconfig/initializers/inflections.rb
createconfig/initializers/mime_types.rb
createconfig/boot.rb
createconfig/environment.rb
createconfig/environments/production.rb
createconfig/environments/development.rb
createconfig/environments/test.rb
createscript/about
createscript/console
createscript/destroy
createscript/generate
createscript/performance/benchmarker
createscript/performance/profiler
createscript/performance/request
createscript/process/reaper
createscript/process/spawner
createscript/process/inspector
createscript/runner
createscript/server
createscript/plugin
createpublic/dispatch.rb
createpublic/dispatch.cgi
createpublic/dispatch.fcgi
createpublic/404.html
createpublic/422.html
createpublic/500.html
createpublic/index.html
createpublic/favicon.ico
createpublic/robots.txt
createpublic/images/rails.png
createpublic/javascripts/prototype.js
createpublic/javascripts/effects.js
createpublic/javascripts/dragdrop.js
createpublic/javascripts/controls.js
createpublic/javascripts/application.js
createdoc/README_FOR_APP
createlog/server.log
createlog/production.log
createlog/development.log
createlog/test.log
D:\myruby>
下面我们进入到这个hello文件夹下。
D:\myruby>cd hello
D:\myruby\hello>
下面我们再创建一个控制器,在这个控制器中可以加入各种相关的动作,也就是action.
D:\myruby\hello>ruby script/generate controller App
existsapp/controllers/
existsapp/helpers/
createapp/views/app
existstest/functional/
createapp/controllers/app_controller.rb
createtest/functional/app_controller_test.rb
createapp/helpers/app_helper.rb
D:\myruby\hello>
这样我们就创建了一个控制器:App.
在D:\myruby\hello\app\controllers文件夹下我们找到这个文件,里面的代码是这样的
class AppController < ApplicationController
end
发现它的意思就是定义一个AppController类,并且继承了ApplicationController这个类,而ApplicationController和它是在同一个文件夹下的。下面是ApplicationController这个类的代码;
# Filters added to this controller apply to all controllers in the application.
# Likewise, all the methods added will be available for all controllers.
class ApplicationController < ActionController::Base
helper :all # include all helpers, all the time
# See ActionController::RequestForgeryProtection for details
# Uncomment the :secret if you're not using the cookie session store
protect_from_forgery # :secret => 'd68461d0ac035694abfc8e7d76febff1'
end
从文件中我们也发现,这些类文件的后缀名是rb,这个就是Ruby的后缀名。
下面我们在这个AppController这个类下创建一个action.名字叫greeting.代码为
class AppController < ApplicationController
def greeting
end
end
从这里我们或许能够领略到Ruby的一些风采。
下面我们再创建一个展示这个action的页面,名字为reeting.rhtml。
下面是这个页面的代码:
<html>
<head>
<title>Ruby on Rails</title>
</head>
<body>
<h1>Yes it’s working!</h1>
</body>
</html>
下面我们来启动这个服务器了。
看一下命令
C:\rubydev\hello>ruby script/server
=> Booting WEBrick...
=> Rails application started on http://0.0.0.0:3000
=> Ctrl-C to shutdown server; call with --help for options
INFOWEBrick 1.3.1
INFOWEBrick::HTTPServer#start: pid=4008 port=3000
OK,大功告成,在浏览器下输入http://localhost:3000/app/greeting
如果在页面上能显示Yes it’s working!那么恭喜你,你已经完成了第一个ROR的程序了。
页:
[1]