文章列表
 
您正在查看 "ruby / rails" 分类下的文章

2008年08月17日 星期日 上午 10:40
作者:老王

假设你已经安装了Ruby,并且设置好了PATH环境变量,那么接下来安装Rails是很简单的:

gem update --system
gem install --include-dependencies rails

如果想在Rails应用里使用MySQL数据库的话,则:

gem install mysql

相应的在创建项目的时候使用命令:

ruby -d mysql 项目名称

不过新版Rails已经使用SQLite3作为缺省数据库类型了,所以下面看看怎么配置:

先去下载sqlite.dll,并放到windows/system32目录,或者PATH里指定的目录,以便系统能够找到它。

然后安装sqlite3-ruby:

gem install sqlite3-ruby

但是此时多半会出现类似下面的错误:

Microsoft Windows XP [Version 5.1.2600]
(C) Copyright 1985-2001 Microsoft Corp.

C:\Documents and Settings\wangbo>gem install sqlite3-ruby
Building native extensions. This could take a while...
ERROR: Error installing sqlite3-ruby:
        ERROR: Failed to build gem native extension.

C:/ruby/bin/ruby.exe extconf.rb
checking for fdatasync() in rt.lib... no
checking for sqlite3.h... no

nmake
'nmake' is not recognized as an internal or external command,
operable program or batch file.

Gem files will remain installed in C:/ruby/lib/ruby/gems/1.8/gems/sqlite3-ruby-1.2.4 for inspection.
Results logged to C:/ruby/lib/ruby/gems/1.8/gems/sqlite3-ruby-1.2.4/ext/sqlite3_api/gem_make.out

出现这个错误的原因是因为系统正在尝试安装一个源代码版本,而不是我们期望的二进制版本。

查看一下有效的sqlite3-ruby软件包都有哪些:

gem list --remote --all sqlite

显示结果如下:

sqlite (2.0.1, 2.0.0, 1.3.1, 1.3.0, 1.2.9.1, 1.2.0, 1.1.3, 1.1.2, 1.1.1, 1.1)
sqlite-ruby (2.2.3, 2.2.2, 2.2.1, 2.2.0, 2.1.0, 2.0.3, 2.0.2)
sqlite3-ruby (1.2.4, 1.2.3, 1.2.2, 1.2.1, 1.2.0, 1.1.0, 1.0.1, 1.0.0, 0.9.0, 0.6.0, 0.5.0)
sqlitecache (0.0.1)

可以看到,我们刚才安装的sqlite3-ruby是1.2.4版本,降低一下版本号,看看是不是有二进制版本:

gem install sqlite3-ruby --version 1.2.3

此时安装应该成功了。
 
2008年08月14日 星期四 下午 4:27
作者:老王

老版本的will_paginate是基于plugin方式的,其安装方式如下:ruby script/plugin install svn://errtheblog.com/svn/plugins/will_paginate

新版本的will_paginate已经抛弃了这样的做法,转而使用gem的方式,本文通过建立一个名为foobar的应用来了解一下will_paginate的用法。

==============================

C:\>rails foobar -d mysql

C:\>cd foobar

C:\foobar>ruby script/generate scaffold article title:string content:text

此时设置一下数据库(C:\foobar\config\database.yml),并且确保相关数据库已经建立好了。

C:\foobar>rake db:migrate

C:\foobar>ruby script/server

此时,http://localhost:3000/articles可以浏览了,添加一些数据(比如十条),为分页做准备。

===============================

plugin形式的旧版will_paginate不用在rails程序里显式的加载,这是因为rails启动时会自动执行插件目录下的init.rb文件,而新版will_pagiante采用的是gem形式,没有这样的机制,所以需要在rails程序里显式的加载:

# C:\foobar\config\environment.rb
Rails::Initializer.run do |config|
   config.gem 'mislav-will_paginate', :version => '~> 2.3.2', :lib => 'will_paginate',
     :source => 'http://gems.github.com'
end

此时,重启一下WEBrick,然后执行如下命令安装相应的gem:

C:\foobar>rake gems:install

安装就完成了。

修改控制器的index方法,加上类似下面的代码:

# C:\foobar\app\controllers\articles_controller.rb
@articles = Article.paginate :page => params[:page], :per_page => 2

修改对应的模板文件,加上类似下面的代码:

# C:\foobar\app\views\articles\index.html.erb
<%= will_paginate @articles %>

再次重启一下WEBrick,然后浏览就可能看到分页效果了。

===============================

will_paginate的最新文档:http://github.com/mislav/will_paginate/wikis
 
2008年02月16日 星期六 下午 9:58
作者:老王

数据迁移中字段的unsigned问题

Rails数据迁移中,对一个int字段,比如说ip字段:

新版本:t.integer :ip
老版本:t.column :ip, :integer

不过这样生成的是int(11),没有设置unsigned,如果一定要用unsigned,可以采用一个权宜的方法:

t.column :ip, "int unsigned"

多少有点怪,而且t.column这样的老式写法已经不推荐了,其实最好的方法还是:

t.integer :ip, :unsigned => true

可惜这种写法不知道啥时候才能支持。(参考链接

数据迁移中的uuid主键问题

老版本中可以这样声明:

create_table :table_name, :id => false do |t|
t.column :id, 'varchar(36) primary key', :null => false
end

新版本不支持,暂时没有办法

表前缀的设置

修改环境配置文件(如:environment.rb)加入一下代码:

config.active_record.table_name_prefix = "article_"

这样,表前缀就被设置成article_了。
 
2008年02月14日 星期四 下午 8:41
作者:老王

在旧版本的数据迁移中,以创建表为例,用法如下:

class CreateProducts < ActiveRecord::Migration
def self.up
    create_table :products do |t|
      t.column :category_id, :integer
      t.column :title, :string
      t.column :description, :text
      t.column :image_url, :string
      t.column :created_at, :datetime
      t.column :updated_at, :datetime
    end
end

def self.down
    drop_table :products
end
end


在新版本的数据迁移中,以创建表为例,用法如下:

class CreateProducts < ActiveRecord::Migration
def self.up
    create_table :products do |t|
      t.references :category
      t.string :title, :image_url
      t.text :description
      t.timestamps
    end
end

def self.down
    drop_table :products
end
end

特别是上面references的用法,让代码有了更好的可读性。散发着DSL的味道。

====================================

具体可以参考相关文档。这里列举常用的方法:

create_table(name, options): Creates a table called name and makes the table object available to a block that can then add columns to it, following the same format as add_column. See example above. The options hash is for fragments like "DEFAULT CHARSET=UTF-8" that are appended to the create table definition.
drop_table(name): Drops the table called name.
rename_table(old_name, new_name): Renames the table called old_name to new_name.
add_column(table_name, column_name, type, options): Adds a new column to the table called table_name named column_name specified to be one of the following types: :string, :text, :integer, :float, :decimal, :datetime, :timestamp, :time, :date, :binary, :boolean. A default value can be specified by passing an options hash like { :default => 11 }. Other options include :limit and :null (e.g. { :limit => 50, :null => false }) — see ActiveRecord::ConnectionAdapters::TableDefinition#column for details.
rename_column(table_name, column_name, new_column_name): Renames a column but keeps the type and content.
change_column(table_name, column_name, type, options): Changes the column to a different type using the same parameters as add_column.
remove_column(table_name, column_name): Removes the column named column_name from the table called table_name.
add_index(table_name, column_names, options): Adds a new index with the name of the column. Other options include :name and :unique (e.g. { :name => "users_name_index", :unique => true }).
remove_index(table_name, index_name): Removes the index specified by index_name.

想要了解细节的话,可以查看源代码,在我的电脑里是如下路径:

C:\ruby\lib\ruby\gems\1.8\gems\activerecord-2.0.2\lib\active_record\migration.rb
 
2008年01月06日 星期日 下午 9:37
作者:老王

在Ruby中,我们可以把一个模块混入(Mixin)到对象中,从而达到类似多重继承的效果。

下面举几个例子来仔细阐述一下这个问题:

首先定义一个Module:

module Foo
def bar
    puts "foo";
end
end


然后我们把这个模块混入到对象中去:

class Demo
include Foo
end


如上编码后,模块中的实例方法就会被混入到对象中:

d=Demo.new
d.bar


会输出foo字样。

下面我们重新定义一下Demo类:

class Demo
extend Foo
end


这个时候如果你得到的就是静态调用:

Demo.bar

会输出foo字样。

下面我们再来重新定义一下Demo类:

class Demo
def bar
    puts "demo"
end
end


然后使用extend方法调用:

d=Demo.new
d.extend(Foo)
d.bar


会输出foo字样。

下面我们再来重新定义一下Demo类:

class Demo
include Foo

def bar
    puts "demo"
end
end


然后使用extend方法调用:

d=Demo.new
d.extend(Foo)
d.bar


会输出demo字样。

其中的味道,自己慢慢体会吧。
 
   
 
 
文章存档
 
     
 
最新文章评论
  

[表情]
 

不错!
 

linux大师之路,www.linuxmr.com
 

引导一直没有整明白说。
 

[表情]
   
帮助中心 | 空间客服 | 投诉中心 | 空间协议
©2012 Baidu