`

Windows下安装Ruby on Rails记录(12月01日update增加YARV的链接)

阅读更多

Windows下安装Ruby on Rails记录

(注:编辑中,未完成, 由于我在RoR方面是纯菜鸟,所以内容可能不是正确的)

1. 了解什么是Ruby on Rails
Rails是一个很出名的Web开发框架,而Ruby on Rails是Rails的Ruby实现。
Ruby则是一种面向对象脚本语言,本身和Rails没有什么必然关系(反倒是RoR让Ruby出名了OTL)

 

2. 大致浏览安装教程
http://rubyonrails.org/download
如果想了解更多,还可以参考官方的wiki(有相应的中文翻译)
http://wiki.rubyonrails.org/

 

3. 安装Ruby 1.8.7/1.9.2。
因为Rails在发展中,版本时常会升级。
到现在为止,我看到的提示是:
We recommend Ruby 1.8.7 or Ruby 1.9.2 for use with Rails. Ruby 1.8.6 and earlier are not supported, neither is version 1.9.1。
它会推荐使用某个版本的Ruby,原因是作为语言的Ruby永远比Ruby on Rails使用的Ruby要新,Ruby on Rails会由于Ruby的版本问题出现bug,所以并非越新的Ruby越好(对于学习Ruby语言当然越新越好,但作为企业级开发来说可能不太安全)如果你对任何东西都心存怀疑,可以使用较旧的绿色版Ruby on Rails,例如InstantRails,安装起来会更加简单(我这里就不讨论绿色版的Ruby on Rails)。
怎么获得Ruby呢,放心,Windows版的Ruby是不需要自己编译的(当然确实有这种需要可以尝试,不过Ruby在Windows下编译并非轻易,我就不讨论了),名字叫RubyInstaller:
http://www.ruby-lang.org/en/downloads/
http://rubyinstaller.org/
如果对win32绿色版Ruby感兴趣,还可参考官网提到的一个网站:
Ruby-mswin32
http://www.garbagecollect.jp/ruby/mswin32/en/

 

(

20101024补充:

http://www.garbagecollect.jp/ruby/mswin32/en/download/release.html

 

20101201补充:

 

YARV: Yet Another Ruby VM(Ruby虚拟机的另一种实现)

 

)

 

 

可以放心的是,windows下的Ruby跟其它平台的Ruby区别并不大(个人觉得OTL)。
再次说明一点,必须注意版本要与Rails兼容,所以我下载了这个版本的Ruby:
Ruby 1.9.2-p0 RubyInstaller
(md5: 21bf42f7ec4b8a831c947d656509cddb)
Stable version (recommended)
安装时最好加入PATH和关联.rb,.rbw。
不加PATH也行,因为这个版本的Ruby带了一个命令提示符的快捷方式Start Command Prompt with Ruby。
可以尝试运行Interactive Ruby(简称irb)
irb(main):001:0> print 'Hello world'
Hello world=> nil
也可以尝试运行Start Command Prompt with Ruby
C:\Documents and Settings\Administrator>ruby -e "print 'Hello world'"
Hello world
(注:Ruby 1.9.2好像无法本地安装旧版本的gem,但可以在线安装,如果需要对比,可以同时装1.8和1.9两个版本的Ruby)

 

 

4. 检查Ruby是否自带RubyGems
RubyGems是标准Ruby包管理器。
http://rubyforge.org/frs/?group_id=126
不过有一点,安装Ruby的时候可能已经有了RubyGems,所以最好在
Start Command Prompt with Ruby下输入gem以查看gem的安装情况:
ruby 1.9.2p0 (2010-08-18) [i386-mingw32]
C:\Documents and Settings\Administrator>gem
RubyGems is a sophisticated package manager for Ruby.  This is a
basic help message containing pointers to more information.
看来RubyGems已经自带了,不需要另行下载。
然后查看已经安装了的本地包
C:\Documents and Settings\Administrator>gem list
*** LOCAL GEMS ***
minitest (1.6.0)
rake (0.8.7)
rdoc (2.5.8)
可以看到Ruby默认是不会帮你安装Rails的(当然啦,因为Rails并不是标准Ruby的一部分,所以Ruby就帮你省了,不过我想可能是因为Ruby的设计者们不愿意受过多的东西拖累)
关于gem的使用手册可以参考这里:
http://docs.rubygems.org/shelf/index

 

 

5. 在线或本地安装Rails。
前面提到的gem命令主要目的就是在线安装Rails。
只要有了gem和可以上网,一切都毫不费力O(∩_∩)O
gem install rails
由于一切都在后台进行没有回显,所以如果你实在不耐烦,可以Ctrl+C结束,然后输入gem list查询已经安装了什么依赖包。(虽然比较麻烦,不过这么做没什么不好)因为逐个下载包时间会比较长,这段时间唯一可以做的是等命令行结束或者是去干别的OTL。
C:\Documents and Settings\Administrator>gem install rails
Successfully installed activerecord-3.0.1
Successfully installed activeresource-3.0.1
Successfully installed mime-types-1.16
Successfully installed polyglot-0.3.1
Successfully installed treetop-1.4.8
Successfully installed mail-2.2.7
Successfully installed actionmailer-3.0.1
Successfully installed thor-0.14.3
Successfully installed railties-3.0.1
Successfully installed bundler-1.0.3
Successfully installed rails-3.0.1
11 gems installed
Installing ri documentation for activerecord-3.0.1...
Installing ri documentation for activeresource-3.0.1...
Installing ri documentation for mime-types-1.16...
Installing ri documentation for polyglot-0.3.1...
Installing ri documentation for treetop-1.4.8...
Installing ri documentation for mail-2.2.7...
Installing ri documentation for actionmailer-3.0.1...
Installing ri documentation for thor-0.14.3...
Installing ri documentation for railties-3.0.1...
Installing ri documentation for bundler-1.0.3...
Installing ri documentation for rails-3.0.1...
Installing RDoc documentation for activerecord-3.0.1...
Installing RDoc documentation for activeresource-3.0.1...
Installing RDoc documentation for mime-types-1.16...
Installing RDoc documentation for polyglot-0.3.1...
Installing RDoc documentation for treetop-1.4.8...
Installing RDoc documentation for mail-2.2.7...
Installing RDoc documentation for actionmailer-3.0.1...
Installing RDoc documentation for thor-0.14.3...
Installing RDoc documentation for railties-3.0.1...
Installing RDoc documentation for bundler-1.0.3...
Installing RDoc documentation for rails-3.0.1...

 

另一种安装方法是把gem文件下载到本地,然后用-l参数安装(不过,这里只是rails-2.2.3,与在线安装的不同)。
例如
> cd D:\ugame\src\rails\rails-install\gem
> d:
> gem install rails -l
下面是需要用到的gem:
Rake 0.8.7
(Rake是Ruby的类make工具。用于数据库迁移,路由校验等)
http://rubyforge.org/frs/?group_id=50
Rails 2.2.3
(Rails是MVC模式的网络应用框架,包含模板引擎、控制器框架和对象关系映射包。开发网络应用的所有东西都可运行于CGI,FastCGI和mod_ruby)
http://rubyforge.org/frs/?group_id=307
Action Mailer 2.2.3
(简化了邮件服务类的创造,使用动作视图(从行动包)来呈现主体模板。管理进出的电子邮件)
http://rubyforge.org/frs/?group_id=361
Action Pack 2.2.3
(简化网络请求路由、处理和响应为half-way前端,half-way页面控制器。实现特别强调简化单元/集成测试而不需要使用浏览器。通过Action Controller和Action View进行HTTP请求的处理和视图)
http://rubyforge.org/frs/?group_id=249
ActiveRecord 2.2.3
(为ORM实现ActiveRecord模式(Fowler, PoEAA)。它把数据库表和类关联为业务对象,如客户或订阅,可以被查找,保存和销毁而不需要手工写SQL)
http://rubyforge.org/frs/?group_id=182
Active Resource 2.2.3
(为ActiveRecord模型类自动生成路由,控制器动作和JavaScript代理对象)
http://rubyforge.org/frs/?group_id=3507
Active Support 2.2.3
(是Rails所需要并普遍使用的实用工具类和扩展的标准库。专门为Rails开发的类)
http://rubyforge.org/frs/?group_id=570
sqlite3-ruby 1.3.1
(SQLite数据库引擎的Ruby界面)
http://rubyforge.org/frs/?group_id=254
注意,全部下载gem格式,并且为2.2.3版本。
测试时,用Ruby 1.9.2本地安装会出错,但用Ruby 1.8.7本地安装则不会:
Ruby 1.8.7-p302 RubyInstaller
(md5: 9391a3dddbbda3ee8aa3bb368fdc5279)
Stable version (recommended)

 

20101022补注:gem下载除了

http://rubyforge.org/

还有

http://rubygems.org/

前者只可以下载Rails 3.0以前的gem,而后者的gem是最新。

 

另外,官网说可以在gem install后接版本号,例如

gem install rails --version 3.0.0.

但尚未验证。

 

 

6. 创建并运行第一个rails。
1)
对于Ruby 1.8.7/Rails 2.2.3,
> rails helloworld
> cd helloworld
> ruby script/server
打开浏览器http://localhost:3000/
点击About your application's environment会报错:
MissingSourceFile in Rails/infoController#properties
no such file to load -- sqlite3
原因是没有安装sqlite3-ruby这个gem或者没有把sqlite3.dll复制到bin目录中。
首先下载并安装sqlitedll-3_7_3.zip(注意是dll)
http://www.sqlite.org/download.html
把dll拷贝到C:\Ruby187\bin
然后本地安装sqlite3-ruby(下载sqlite3-ruby-1.3.1-x86-mingw32.gem,放在某个目录中)
> cd D:\rails-install\gem
> d:
> gem install sqlite3-ruby -l
然后用gem list确定那你已经安装了这个gem。
现在返回到刚才创建的helloworld目录中。
> cd C:\Documents and Settings\Administrator\helloworld
> c:
> ruby script/server
打开浏览器http://localhost:3000/检查页面是否正常

 

补充一下,理论上可以关闭数据库功能。
方法是用编辑器打开config/environment.rb
  # Skip frameworks you're not going to use. To use Rails without a database
  # you must remove the Active Record framework.
  # config.frameworks -= [ :active_record, :active_resource, :action_mailer ]
去掉config.frameworks前的注释,以跳过Active Record安装(你大概一眼就认出,其实这就是个ruby文件)
不过这么做可能会影响你后面使用数据库,也无法消除no such file to load -- sqlite3的错误。
最好的方法还是上面所说的那样装sqlite3-ruby和sqlite3.dll消除这个问题。

 

 

------------------
2)
对于Ruby 1.9.2/Rails 3.0.1,注意rails的用法和1.8的不同,创建需要加new
> rails new helloworld
切换到helloworld目录
> cd helloworld
注意,和1.8.7不同,默认需要安装SQLite3的dll,后面会提到。
运行服务器直接用rails,不需要用ruby了。
> rails server
Could not find gem 'sqlite3-ruby (>= 0, runtime)' in any of the gem sources.
Try running `bundle install`.
此时运行bundle install
> bundle install
安装sqlite3-ruby后
> rails server
提示找不到sqlite3.dll
下载并安装sqlitedll-3_7_3.zip(注意是dll)
http://www.sqlite.org/download.html
把sqlite3.dll解压到C:\Ruby192\bin下
> rails server
打开浏览器http://localhost:3000/检查页面是否正常

 

 

 

7. 用rails创建第一个数据库表

数据库作为服务器的后端,是最容易理解同时也是最难驾驭的,所以我选择从这里找突破口。

有过网站或网游开发经验的人都知道,从需求到产品的开发过程有时是从数据建模开始入手的。

说得专业点就是MVC设计模式。Rails试图使用ORM技术来简化这种传统工作流程。

和其它工具有点不同,rails默认是用sqlite3的。(幸运的是,我对sqlite一无所知)

所以我下面都是基于sqlite3来说的。(按道理来说其它数据库是共通的)

由于rails认为所有工程都是依赖于数据库的(汗),所

以它在创建空白工程时自动生成config/database.yml。

它很特别,因为文件夹里只有它不是.rb后缀。

通常不需要修改它,除非你用的不是sqlite3,例如

development:

adapter:mysql

database:mydiary

username:mydiary

password:mypassword

host:localhost

这里不讨论mysql,但有必要清楚知道development才是目前需要小心的设置。

传统的MVC做法是手工创建数据库的表结构,然后在处理中进行转换。

但rails希望数据库的表结构是自动创建的。

不同于用sql语句创建表的方式,rails把操作拆为两步,如同模板开发那样。

1)对于Rails 2来说:

> ruby script/generate migration AddEntriesTable

      create  db/migrate

      create  db/migrate/20101124180351_add_entries_table.rb

注意AddEntriesTable只是个随意的名称,并不需要太在意。

> explorer .

编辑db/migrate目录下刚生成的.rb文件

class AddEntriesTable < ActiveRecord::Migration

  def self.up

  end

 

  def self.down

  end

end

改为

class AddEntriesTable < ActiveRecord::Migration

  def self.up

create_table :entries do |table|

table.column :title, :string

table.column :content, :text

table.column :created_at, :datetime

end

  end

 

  def self.down

drop_table :entries

  end

end

有几点需要注意的是:

entries是要操作的表名,在ruby中字符串写成:entries可以提高效率。

列名和类型需要给出(好像sql语句那样)

up对应表的创建和迁移,down对应表的回滚。

id列式隐式创建的。

最后,执行创建和迁移

> rake db:migrate

(in C:/Documents and Settings/Administrator/helloworld)

==  AddEntriesTable: migrating ================================================

-- create_table(:entries)

   -> 0.0781s

==  AddEntriesTable: migrated (0.0781s) =======================================

2) 对于Rails 3来说,命令行变成:

> rails generate migration AddEntriesTable

      invoke  active_record

      create    db/migrate/20101124182449_add_entries_table.rb

(编辑.rb文件,同上,略)

> rake db:migrate

(in C:/Documents and Settings/Administrator/test/helloworld)

==  AddEntriesTable: migrating ================================================

-- create_table(:entries)

   -> 0.0000s

==  AddEntriesTable: migrated (0.0000s) =======================================

 
 

 

 

8. (待续)

 

 

参考资料:

1. RoR主页
http://rubyonrails.org/


2. 《Rails学习手册》(Learning Rails)
Learning Rails
Rails from the Outside In
By Simon St. Laurent, Edd Dumbill
http://oreilly.com/catalog/9780596518783

 

3. 《Ruby从入门到精通》(Beginning Ruby:From Novice to Professional)

 

 

 

 

更新日志:

2010年12月01日 增加YARV的链接

2010年11月25日 增加数据库表创建

2010年10月24日 修改关于mswin32的ruby二进制发布版下载链接

2010年10月22日  关于gem下载的补充

 

 

 

 

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics