星期二, 7月 17, 2007

Installing RMagick on Ubuntu

Before install RMagick, we need to install ImageMagick and the delegate libraries used by ImageMagick.

Here are some recommended delegate libraries:

  • FreeType, Version 2.0 or above, to annotate with TrueType and Poscript Type 1 fonts.
  • libjpeg to read and write JPEG v1 format images.
  • The PNG library, to read and write PNG format images.
  • libwmf 0.2.5 or later, to read and write Windows Meta File (WMF) format images.
  • Ghostscript version 8.10, to read and write the PDF and PS document formats.
For the default Ubuntu 7.0.4 installation, I have to install FreeType v2 from a tarball. For the libjpeg, libpng, and libwmf, the runtime library are installed, so I just need to install the development library. Since I am too lazy, I just install those thru Synaptic Package Manager.

For the ghostscript, remember to install fonts into /usr/share/ghostscript; otherwise RMagick will complain during compilation.

Step 1: Install ImageMagick

Go to http://www.imagemagick.org and download the latest version of the software to a temporary directory.
tar xvzf ImageMagick.tar.gz
cd ImageMagick-X.Y.Z
Then configure ImageMagick
./configure --disable-static --with-modules --without-perl \
--without-magick-plus-plus --with-quantum-depth=8
Once you have configured ImageMagick the way you want it, enter these two commands:
make install
sudo make install

Step 2: Install RMagick

Issue the command

sudo gem install rmagick
You will see this output:
Bulk updating Gem source index for: http://gems.rubyforge.org
Building native extensions. This could take a while...
Successfully installed rmagick-1.15.7

Be patient. A lot of time will pass with no output from the gem command. The "Successfully installed" message does not mean that RMagick was successfully installed. The RMagick installation can encounter error conditions that gem can't detect. The following irb session is a better indicator of a successful install.

$ irb -rubygems -r RMagick
irb(main):001:0> puts Magick::Long_version
This is RMagick 1.15.7 ($Date: 2007/06/09 16:45:25 $) Copyright (C) 2007 by Timothy P. Hunter
Built with ImageMagick 6.3.5 07/17/07 Q8 http://www.imagemagick.org
Built for ruby 1.8.6 (2007-03-13 patchlevel 0) [i686-linux]
Web page: http://rmagick.rubyforge.org
Email: rmagick@rubyforge.org
=> nil

The RMagick HTML documentation is automatically installed at
/usr/local/share/RMagick/index.html.


Done!!!

星期五, 7月 13, 2007

Using GMail SMTP in ActionMailer

Rail's ActionMailer was simply the automatic choice, since I am building a rails app.

Turns out GMail supports only SSL SMTP mailing service, meaning if you cannot create a SSL connection to its SMTP server, you cannot send email through them. DHH writes about how to do so through installing msmtp here, but we developers just obviously love more choices.

Anatol Pomozov has developed a library called smtp_tls.rb . It allows us to use SMTP Server with TLS. Just follow the instruction in the original post, and then insert

require ‘smtp_tls’

under config/enviroment.rb

Finally, under config/enviroments/xxxxx.rb to specify Gmail settings

ActionMailer::Base.server_settings = {
:address => "smtp.gmail.com",
:port => "587",
:domain => "localhost.localdomain",
:authentication => :plain,
:user_name => "your Gmail account",
:password => "your Gmail Password"
}

星期四, 6月 28, 2007

Discovering a world of Resources on Rails

Here is a very good presentation by David Heinemeier Hansson about RESTful in Rails.


星期日, 6月 10, 2007

Installing RMagick on Windows

Tried out RMagick demo script by installing on Windows (Why on Windows? My project needs this platform.). At first shot, the demo script demo.rb simply doesn't work. It gave an error like this.

Can't convert String into Integer (TypeError)
Got a few iterations by uninstalling and reinstalling Ruby and RubyGem. Finally found out that it's the mismatch with RubyGem starting from version 0.9.3 onwards. So, to this moment, what I could do is to keep my gem version to 0.9.2 (Ruby at 1.8.6 is alright).

星期四, 6月 07, 2007

I'm Ruby on Rails, I'm Java

I really love this video from railsenvy.com. The idea is come from Get a Mac TV Ads by Apple.com

Some interesting comparison between PHP, Rails and Java

During the International PHP Conference 2006, Tim Bray, who - among many other things - co-edited the XML 1.0 and XML namespace definitions, gave a keynote about "How to combine PHP technology with Java based on Enterprise Systems". During his keynote, he presented some very interesting comparison between the popular development "frameworks" PHP, Ruby on Rails (RoR, Rails) and Java


  • Scaling : Load balancing, Shared-nothing, CPU , DBMS, File I/O, Observability
  • Development Speed : Compilation step, Deployment step, Code Size, Configuration process
  • Development Tools : IDE, Templating, How many tools, O/R Mapping, Performance, Documentation
  • Maintainability : MVC, OO, Readability, Language count, Code size
It seems Java can't get a upper hand from the eyes of Sun people.

Download the presentation file

Why I love RoR?

I agree most of those from analysis and comparison. In fact, I just want to point out that there are several good IDE are coming out e.g. Aptana IDE (RadRails has integrated into Aptana)

Pros

  • An MVC framework for Ruby helps separate presentation from business logic
  • Unit tests are built right in.
  • Don’t repeat yourself (DRY) principle makes for less painful development
  • Agile no compile development
  • Convention over configuration and meta-programming does away with all the configuration you have in a Java framework like Struts.
  • Active Record Object Relational Mapper needs very little mapping configuration.
  • Heavy Ajax Support, Prototype and Scriptaculous helpers are in Rails
  • Built in XML Web Services
  • The Ruby language is pretty easy to learn. Everything is an object. Its clean and easy to read.
  • Easy model validations
  • Very quick to develop applications from the ground up when you have control over the database schema
  • Share nothing architecture. Session data is stored on disk or in the database so it could be shared amongst many nodes
  • Ruby is dynamically typed, which allows for easier development in my opinion since one can just check to see if an object has a method instead of worrying about what kind of type it actually is.
  • Classes are never closed so you can easily add methods to any class, even other apis. This allows active record to dynamically create method names for each database columns at run time. There is no need to update a mapping schema when you add a column to the database, the method is “magically” there.
  • Closures allow for dynamic behavior that would take much more code in Java.
  • It protects developers from common mistakes. It follows best practices which should be adhered too in new development. Also, since it scales by process you don’t have to worry about developers trying to create threads in the application container which could bring down the container.
  • Mongrel Server gives a Rails application its own container and speeds production requests greatly.
  • Ruby has been around for some time, first developed in 1993. Ruby is a language in itself was was not created to be web specific.
Cons
  • Limited legacy schema support. You pay in added code for someone else using composite primary keys in the database schema. Not a show stopper, but you lose the quick development features of the ActiveRecord database model each time you have to account for this.
  • Freely available api documentation is seriously lacking. Books are fairly decent but they are having a hard time keeping up with the rapidly evolving framework. It makes it hard to know about deprecated practices.
  • If you cannot follow the best practices for some reason you are forced to solve the issues by hand.
  • Generated Scaffold code is pretty useless when working with a database that doesn’t follow the rails specs. You have to customize a great deal, then again the word is that we are not supposed to be using scaffolds for real development, which was one of the main selling points of rails.
  • Scaling by process can eat a lot of memory on the server. Memory is cheap though.
  • No built in internationalization support.
  • No plan to include better legacy database support.
  • SQL Server(all versions) support is very lacking and has the same problems just as PHP. I would not use it for production against SQL Server.
  • Rails is a fairly new framework compared to PHP and Java, so there are not as many developers, but many are eager to learn.
  • Limited IDE environments. Textmate is the best but it only runs on a Mac.

星期三, 6月 06, 2007

Acts_As_Ferret Tutorial

Here is a list of good tutorials on acts_as_ferret

  • From Gregg Pollack, he covers all the important features of Ferret/ActsAsFerret - from simple searches to custom fields to match highlighting. Click Here
  • From Roman Mackovcak, a nice introduction to Acts_as_ferret including info on how to do paging across search results. Click Here

星期二, 6月 05, 2007

acts_as_ferret: Rails全文搜尋快速上手(與中日韓文支援)

相信許多鐵道迷都聽過雪貂(Ferret)。雪貂是一套根據Lucene所開發的全文搜尋引擎。裝上了「化身為雪貂」(acts_as_ferret)這套plug-in之後就更厲害了,任何ActiveRecord model只要加上輕量之人最愛的神秘一行,瞬間就具有了全文搜尋能力。

Ferret是用C寫成的,用語和基本觀念與Lucene一致。因此對Lucene有認識的朋友應該很容易上手。雖然說化身為雪貂很好用,不過O’Reilly的Ferret一 書仍有一讀的必要。該書最後還介紹如何配合其他plug-in來index諸如PDF, JPEG EXIF等metadata,幾乎可以寫一套小型的Mac OS X Spotlight。而該書對於Ferret的構成、內部運作原理、performance tuning的介紹,也相當實用(而且,不需要先學Lucene;我也還在研讀這一部份就是了……)。

以下是我用的 RegExpAnalyzer,僅僅很簡單的把歐語的單詞拆開、數字拆開,中日韓文則以字元方式來索引。這種簡單的中日韓文tokenizing在搜尋精 確度不要求高的場合,大體能用。要更好的搜尋結果,或是要做到同音字搜尋、簡繁搜尋,當然就需要更複雜的 Analyzer。

請找個地方填入以下的 regex 跟 Analyzer:

GENERIC_ANALYSIS_REGEX = /([a-zA-Z]|[\xc0-xdf][\x80-\xbf])+|[0-9]+|[\xe0-\xef][\x80-\xbf][\x80-\xbf]/
GENERIC_ANALYZER = Ferret::Analysis::RegExpAnalyzer.new(GENERIC_ANALYSIS_REGEX, true)

然後在想要加入搜尋的 model 裡加入:

acts_as_ferret({:fields => [ FIELDS_YOU_WANT_TO_INDEX ] }, { :analyzer => GENERIC_ANALYZER })

之所以不把 GENERIC_ANALYZER 放在 acts_as_ferret 裡,除了可重用性的原因,另外還避免掉在 Mongrel + Rails development mode 時可能造成的 bus error / segmentation fault (原因不明)。

總之,只要做完這件事,就可以:

Model.find_by_contents("hola")

acts_as_ferret很聰明,如果是第一次使用,會幫你把這個data model所用table全部讀一遍,建立必要的全文索引。之後所有的 CRUD 動作只要透過這個 model ,「化身為雪貂」會幫你做完所有該做的 Ferret indexing 動作。

先前有一些中文論壇提到用 /./ 來處理中文斷字(說「斷詞」或「分詞」有誤導之嫌)。雖然 Ruby 的 regex engine 在 $KCODE 設為 utf-8 時,可以正確地以 /./ 來掃描 Unicode 字元,但是這樣的作法是有問題的。英文詞因此會被斷成一個字元一個字元。而,單純用 [a-zA-Z] 則忽略了歐語,這是不夠的。

偏偏 Ruby 的 Unicode 支援只做了一半,不像 Perl 可以用 /\x{80}-\x{7ff}/ 的方式來表達 Unicode range,所以我們得祭出 jcode.rb 裡處理 UTF-8 的 regex (也就是利用 UTF-8 的特性),來找出實際上為 U+80 ~ U+7FF 以及 U+800 ~ U+FFFF 的字元。當然,> U+FFFF 的字元這裡並沒有處理,而且這個方式其實過於簡化。

但總之這是可以用的方法。如果要測試或改善 regex ,可以使用Ferret一書中第65頁所列的以下方法來測試:

def test_token_stream(token_stream)
puts "Start | End | PosInc | Text"
while t = token_stream.next
puts "%5d |%4d |%5d | %s" % [t.start, t.end, t.pos_inc, t.text]
end
end

然後在irb中:

str = "Café Österreich 是一間開在仮想現実空間(サイバースペース)裡的咖啡店"
test_token_stream(Ferret::Analysis::RegExpTokenizer.new(str, GENERIC_ANALYSIS_REGEX))

就可以看到 RegExpTokenizer 執行的效果。

* Update: lingr.com has released their multilingual analyzer, click here for detail

Unicode issue in MySQL

If you want to create a database with Unicode support, please use the following command to create the database

create database [database-name]
character set utf8 collate utf8_unicode_ci;
After that, in you Rails application, inside the database.yml, please add
encoding: utf8

星期日, 6月 03, 2007

1 minute Ruby on Rails lecture

You have 60 seconds to read it. Tic...Tac...Tic...Tac.... Time's Up. Got it!


Ruby on Rails
Originally uploaded by WanCW.


星期四, 4月 26, 2007

用 acts_as_ferret 進行搜索的心得

下載ferret.gem , acts_as_ferret plugin
gem : gem install ferret
plugin : svn://projects.jkraemer.net/acts_as_ferret/trunk/plugin/acts_as_ferret,直接check out下來放到vendor/plugin 目錄下即可
現有一個model:

class Provider< fields =""> [:name,:description,:address,:website,:score,:phone,:direction]
.......... ............
end

這裡:fields就是要進行索引的對象屬性了,當然你也可以不指定,直接就一句"acts_as_ferret",默認不指定的時候是對所有列進行索引,先時事效果:

>>ruby script/console
>>Provider.find_by_contents(" 你要 搜索 的內容")

OK,如果沒有意外的話, 結果會返回一個ActsAsFerret::SearchResults對象。如果出錯了,回過去檢查一下,短短幾步,檢查起來也應該很方便。

幾個tip:

# "關鍵詞1" AND “關鍵詞2”
Provider.find_by_contents(" 關鍵詞1 關鍵詞2")
#"關鍵詞1" OR "關鍵詞2"
Provider.find_by_contents("關鍵詞1 OR 關鍵詞2")
#貌似“關鍵詞1”的結果
Provider.find_by_contents("關鍵詞1~")
上面這幾個都是小兒科啦

下面我們到acts_as_ferret plugin內部去看看,
acts_as_ferret/lib 瞄到 acts_as_ferret.rb 第116行(VERSION=181)
“ActiveRecord::Base.extend ActsAsFerret::ActMethods”
哈哈,許多blog中都建議在要進行分詞查詢的model中加入下面一段代碼:

def full_text_search(q, options = {})
return nil if q.nil? or q.strip.blank?
default_options = {:limit => 10, :page => 1}
options = default_options.merge options
options[:offset] = options[:limit] * (options.delete(:page).to_i-1)
results = self.find_by_contents(q, options)
[results.total_hits, results]
end
不過,本著DRY的原則,我決定從“ActiveRecord::Base.extend ActsAsFerret::ActMethods”這句話上做文章,既然ActiveRecord::Base 繼承了 ActsAsFerret::ActMethods ,何不將這段代碼加入到ActMethods裡去,讓所有model都擁有 full_text_search的功能?

module ActsAsFerret #:nodoc:

# This module defines the acts_as_ferret method and is included into
# ActiveRecord::Base
module ActMethods

def full_text_search(q, options = {})
return nil if q.nil? or q.strip.blank?
default_options = {:limit => 10, :page => 1}
options = default_options.merge options
options[:offset] = options[:limit] * (options.delete(:page).to_i-1)
results = self.find_by_contents(q, options)
[results.total_hits, results]
end

................ ....................

end
end
重啟一下,Provider.full_text_search方法應該出來了,有問題的話自己回去檢查看看
下面就是俗套,在application.rb中加入:
def pages_for(size, options = {})
default_options = {:per_page => 10}
options = default_options.merge options
pages = Paginator.new self, size, options[:per_page], (params[:page]||1)
pages
end
這些種種工作都是為了在controller以及view中調用:
controller:
def search @query=params[:query]
@total, @providers = Provider.full_text_search(@query, :page => (params[:page]||1))
@pages = pages_for(@total)
end

view:
<%= pagination_links(@pages, :params => {:query=>@query}) %>

關於 find_by_contents(q, options = {}, find_options = {})
中的find_options,考慮一下一種情況:
provider表結構:
id name description category_id

如果要對特定category_id的數據進行檢索,應如何?這裡就用到了find_option了:
代碼:
Vendor.find_by_contents(" 要進行檢索的關鍵字 ",{:limit=>30},{:joins=>"providers join categories on providers.category_id = categories.id ", :conditions =>" providers.category_id = XXXXX "})
其他一些小技巧大家可以到到網上找找,這裡就不一一列舉了。

星期一, 4月 23, 2007

Ruby vs. Java

星期三, 4月 11, 2007

Ruby Amazon E-Commerce REST Service API (amazon-ecs)

Here is a generic Amazon E-commerce REST API for ruby. It supports configurable default options and method call options. It uses Hpricot to parse the XML output. Use Response and Element wrapper classes for easy access to the XML elements, and it supports ECS 4.0. read more ...

星期一, 4月 02, 2007

Ruby on Rails Security Blog

Ruby on Rails Security Blog is a new blog for addressing security issues about ror. However, most of the topics are related to MySQL currently.

星期四, 3月 29, 2007

TIOBE Programming Community Index for March 2007

# in Mar
2007
# in Mar
2006
Delta in
Position
LanguageRatings
Mar 2007
Delta
Mar 2006
Status
1 1 Java 18.044% -3.84% A
2 2 C 15.633% -2.16% A
3 3 C++ 11.109% -0.05% A
4 4 PHP 9.458% -0.49% A
5 5 (Visual) Basic 8.147% -1.74% A
6 6 Perl 6.420% 0.00% A
7 8 Python 3.897% +0.80% A
8 10 JavaScript 3.485% +1.75% A
9 7 C# 3.365% +0.22% A
10 21 11 * Ruby 2.773% +2.31% A
11 11 SAS 1.849% +0.51% A
12 9 Delphi 1.760% -0.08% A
13 12 PL/SQL 1.549% +0.56% A
14 20 6 * D 1.268% +0.78% A
15 19 ABAP 0.777% +0.26% A
16 15 Lisp/Scheme 0.726% +0.15% A-
17 14 Ada 0.654% +0.06% B
18 17 COBOL 0.647% +0.12% B
19 16 FoxPro/xBase 0.600% +0.06% B
20 31 11 * Transact-SQL 0.586% +0.38% B


Ruby goes up for 11 postion and get the highest growth rate (+2.31%) among other programming languages. I beleive there is lot of potential of Ruby. According to TIOBE,
Both languages are boosted by their corresponding frameworks, Ruby On Rails and Ajax. This might be a new trend. In the recent past it was necessary to have a large company behind the language to get it in the spotlight (Sun with Java, Microsoft with C#), but nowadays a killer app appears to be sufficient. Viral marketing via the Internet works!

星期一, 3月 26, 2007

Apache 2.2 + Mongrel 設定方式

設定 Mongrel Cluster

請將你的 Mongrel Cluster 設定好,這裡預設 port 從 4000 ~ 4009 ,一共十個,跑在 production 環境下

mongrel_rails cluster::configure -e production -p 4000 -N 10
mongrel_rails cluster::start
修改 Apache 2.2 設定檔

Apache 的設定檔放在 httpd.conf ,以下修改內容皆在 httpd.conf 裡面設定。

首先確定你的 Apach 2.2 有 enable apache 的其中一個 Module mod_proxy
LoadModule proxy_module modules/mod_proxy.so
LoadModule proxy_balancer_module modules/mod_proxy_balancer.so
LoadModule proxy_http_module modules/mod_proxy_http.so
再來我們開始設定 Mongrel Cluster 的 reverse proxy 設定,首先我們給這組 cluster 取名叫做 examplecluster,他是10組 Mongrel ,跑在 port 4000 ~ 4009 之中

# cluster member 1
BalancerMember http://127.0.0.1:4000
BalancerMember http://127.0.0.1:4001
....
BalancerMember http://127.0.0.1:4009
再來假設你的 hostname 為 example.com,我們開始設定 virtual host

ServerName example.com
ServerAdmin root@example.com
DocumentRoot "/var/www/example.com/htdocs"
ProxyPass /images !
ProxyPass /stylesheets !
ProxyPass /javascripts !
ProxyPass / balancer://examplecluster/
ProxyPassReverse / balancer://examplecluster/


重 點是在於 ProxyPass,ProxyPass 代表 images,stylesheets,javascript 等 static file 交給 Apache 處理,不要給 Mongrel 處理。 還有 ProxyPassReverse 要指定正確的 cluster name ,我們要指定為 examplecluster。

最後重起 Apache 即可。本設定改自著名的文章 Scaling Rails with Apache 2.2, mod_proxy_balancer and Mongrel,還有 Robin 的 在Windows平台使用Apache2.2和Mongrel运行Ruby on Rails。設定是在 Gentoo Linux + Apache 2.2.3 + Mongrel 跑完全沒有任何問題。

[via: http://lightyror.thegiive.net/2006/12/apache-22-mongrel.html]

Mongrel 使用方式 Part 2 : Mongrel Cluster

看完上一篇,大家一定很想丟雞蛋
這樣的 tutorial 也可以出來混一篇文章
在大家浪費買蛋錢之前,我必須要說,好戲在後頭呀!!!
Mongrel 如果只有單獨啟動在 80 port
他充其量不過是一個速度較普普的 Web Server
但是,當作 Backend Application Server 才是他的宿命呀

今天的需求是這樣
我們可以開啟數個 Mongrel Process 在其他 port
前端開一個 Proxy Server
當 Proxy Server 接受到 request
他會傳給 backend application server
其實作法很像 Mod_fastcgi 的作法
只是中間的 application server 將 fastcgi 改成了 mongrel

如果要開啟眾多的 Mongrel Process
我們當然可以用

mongrel_rails start -d -p 8000 -P log/mongrel_1.pid
mongrel_rails start -d -p 8001 -P log/mongrel_2.pid
....
這樣來執行,但是要管理就變得相當的麻煩
不但要一一起動,要關掉或是重起的變得相當麻煩
如果 Mongrel 那麼麻煩的話
那我們還搞屁,直接用 fastcgi 就好啦 XD
這時候請用 mongrel cluster
有了他,Mongrel 才變成簡單好用的 Application Server

安裝方式
gem i mongrel_cluster
即可

設定方式
一開始,請到 Ruby on Rails 的根目錄
先設定 Mongrel 的設定檔
mongrel_rails cluster::configure -e 那些環境 \
-p 8000 \
-N 3 \
-c /var/www/servers/ \
-a 127.0.0.1 \
--user mongrel \
--group mongrel
他會將 config 寫到 config/database.yml
我解釋一下選項
  • -e 就是用那個環境啟動(production/development/test)
  • -p 就是用那個 port 開始
  • -N 就是開啟幾個 process
  • -c 就是先切到那個目錄,再執行 mongrel cluster (通常是這個Ruby on Rails 的根目錄)
  • -a 就是在那個 host 啟動
  • --user 就是以那個 user 身份啟動
  • --group 就是以那個 group 啟動

如果設定好後,以後使用 mongrel_rails cluster::start 他就會立刻使用剛剛的設定檔
這裡要注意的是 -N 跟 -p 的選項
假設 -p = 8000 -N 為 3
那他會開啟三個mongrel process ,分別以 8000 , 8001 , 8002 來聽
其實 -p 8000 -N 3
就跟
mongrel_rails start -d -p 8000 -P log/mongrel_1.pid
mongrel_rails start -d -p 8001 -P log/mongrel_2.pid
mongrel_rails start -d -p 8002 -P log/mongrel_3.pid
一樣的意義

以此類推假設 -p = 7000 -N 為 5
那他會開啟五個mongrel process ,分別以 7000 , 7001 , 7002 , 7003 , 7004 來聽

使用方式
啟動方式
mongrel_rails cluster::start

關閉方式
mongrel_rails cluster::stop

重起方式
mongrel_rails cluster::restart

參考連結
  1. Rails, Mongrel, Lighty and Mint

[via: http://lightyror.thegiive.net/2006/10/mongrel-part-2-mongrel-cluster.html]

Mongrel 使用方式

Ruby on Rails 架設一直有一個問題
找不到適當的 Application Server
Webricks 太慢,fastcgi 太玄妙,scgi 太年輕
這個時候
只有 Mongrel 可以拯救世界呀~~~~

Mongrel 首頁是這樣寫的

Mongrel is a fast HTTP library and server for Ruby that is intended for hosting Ruby web applications of any kind using plain HTTP rather than FastCGI or SCGI. It is framework agnostic and already supports Ruby On Rails, Og+Nitro, and Camping frameworks.
他跟Webricks 一樣,是一個方便我們架設 Ruby on Rails 環境的網頁伺服器
並且他比 Webricks 還要來得快速

安裝方式很簡單
gem i mongrel
使用方式呢,基本上跟 Webrick 差不多
到 Rails 根目錄
打入
mongrel_rails start
你就會發現他已經可以使用了

如果要用 deamon 模式來使用的話
打入
mongrel_rails start -d
即可

如果要使用特定的 port
mongrel_rails start -p 1234
即可

用不同環境啟動
mongrel_rails start -e production/development/test
即可

指定log file
mongrel_rails start -l log/mongrel_log
即可

有啟動的問題
mongrel_rails start -h
即可

重起 Mongrel
mongrel_rails restart

停止 Mongrel
mongrel_rails stop

[via: http://lightyror.thegiive.net/2006/10/mongrel.html]

ror installation

My steps to install Ruby On Rails 1.8.5 over ubuntu 6.0.6

Prep
Make sure you have gcc complier and zlib development package installed. If not sure, please run

apt-get install build-essestial
apt-get install zlib1g-dev
Ruby
Download ruby-1.8.5-p12.tar.gz from http://www.rubyonrails.org
tar xzvf ruby-1.8.5-p12.tar.gz
cd ruby-1.8.5-p12
./configure --prefix=/usr/local --enable-pthread
make
sudo make install
sudo make install-doc
cd
..
If you saw lots of text fly by but didn’t get that error, it means that we should now have a brand new Ruby installed.

We can verify this (as well as a correct path setting) by typing the following command:

ruby -v
You should see something like this:
ruby 1.8.5 (2006-12-04 patchlevel 2) [i686-linux]
zLib
Download zlib package from
http://www.blue.sky.or.jp/atelier/ruby/ruby-zlib-0.6.0.tar.gz
tar zxvf ruby-zlib-0.6.0.tar.gz
cd ruby-zlib-0.6.0
ruby extconf.rb
make
sudo make install
cd ..
RubyGems
Download rubygems package rubygems-0.9.2.tgz from
http://rubyforge.org/projects/rubygems/
tar xzvf rubygems-0.9.2.tgz
cd rubygems-0.9.2
sudo /usr/local/bin/ruby setup.rb
cd ..
RubyOnRails
With RubyGems installed, Rails is a simple, one-line install:
sudo gem install rails --include-dependencies
* gem respects the following variables for proxy settings
  • http_proxy
  • HTTP_PROXY
  • NO_PROXY

星期日, 3月 25, 2007

Ruby on Rails 伺服器架設原理

一般來說,Ruby on Rails 架設原理很簡單,分成三個部份。

  1. Frontend Server
  2. Application Server
  3. Database Server
每個部份都有自己的功用。


Frontend Server 負責將所有 HTTP Request forward 給後端的 Application Server,也就是他是作類似 Reverse proxy 的工作。一般來說,Apache 2.2 跑 mod_reverse_proxy 是最常出現的選擇,nginx 也是不錯的選擇。Lighty 1.5 之後跑 mod_proxy_core 也很方便,等到 1.5 release 之後我會作詳細的評估。
Application Server 負 責跑 Ruby on Rails 程式,你寫好的程式就在上面跑。一般來說可以用 Webricks,Fastcgi,scgi,Mongrel 等等 Ruby on Rails Runner來跑。Webricks 太慢,Fastcgi 有穩定性,以及不好設定的疑慮。SCGI 太年輕。目前最穩,最好用,而且已經被驗證過的 Application Server 就是 Mongrel。

Database Server 就是處理資料處理的工作。可以跑 SQLite,MySQL 之類的 RDBMS 。甚至使用 File 來當資料處理,或是使用 NFS 也未嘗不可。不過這裡通常大家都是使用一般大廠的 RDBMS資料庫。

組合方式

Apache 2.2 + Mongrel:穩定,高彈性的組合

Apache 2.2 遇到 user request 用 HTTP 分配給 Mongrel ,然後 Mongrel 去跑。這個組合的好處是每個 Application Server node 都可以很輕易的拆成不同台機器,並且很好管理。Apache 的穩定又是出名的。雖然不見得最快,但是方便 scale 以及穩定是他最好的優點。目前所有組合當中最穩定的方式,很多流量最大的 Ruby on Rails 站台也都是使用這個方式。適合在已經有一定規模的網站。設定方式在此我有介紹

Lighttpd + Fastcgi:單機最佳選擇
Lighttpd 1.4 跑 PHP 或是跑 Ruby on Rails 都是這樣跑。Lighttpd 為 Frontend Server ,他接到 request ,經由 unix socket 送給身為 Application Server 的 Fastcgi ,Fastcgi 再執行 Ruby on Rails 程式。通常這個方式是 Frontend Server 跟 Application Server 跑在同一個機器上面的,當然每個 Fastcgi 也可以跑在 TCP Mode ,然後 Fastcgi 放在不同機器上面,不過設定不易不常使用這個方式。這個組合的好處是執行效率非常快速,如果 Frontend 跟 Application 放在同一台機器上,Lighty 這個組合遠比 Apache 的組合來得輕快許多。但是缺點是多台機器設定複雜,不易擴展到多台機器上,Fastcgi 又有在高負載下罷工的負面報導。相反的,單台機器上面 Fastcgi 設定管理簡單,速度又飛快,非常適合小網站剛剛起步時,機器不足,流量不大的需求。設定方式在此我有介紹

Nginx or Lighttpd 1.5 + Mongrel:未來的新選擇

類 似Apache 的組合,但是 nginx 或是 Lighttpd 1.5 都有一個優點,不像 Apache 2.2 那麼肥。反正這個 Frontend 只要作 reverse proxy 的工作,其實不需要 Apache 那麼大的 Server 來作。好處是 Frontend Server 比較快速。壞處是 nginx 這個俄國來的 Server 大家不熟悉,doc 又很少(俄文是很多啦)。Lighttpd 1.5 又只在 pre-release 階段,穩定程度還是得花點時間。不過如果扣掉 Frontend 的 X factor,這個組合兼具輕快跟方便 Scale ,可說是最好的組合。lighty 1.5設定方式在此我有介紹。nginx 的設定檔這裡有範例,可以參考。

[via: http://lightyror.thegiive.net/2006/12/ruby-on-rails_30.html]

星期四, 2月 22, 2007

DB Schema for acts_as_taggable_on_steroids


class AddTagSupport < force =""> true do t
t.column :name, :string
end

create_table :taggings, :force => true do t
t.column :tag_id, :integer
t.column :taggable_id, :integer
t.column :taggable_type, :string
t.column :created_at, :datetime
end

create_table :products_tags, :id => false, :force => true do t
t.column :tag_id, :integer, :default => "0", :null => false
t.column :product_id, :integer, :default => 0, :null => false
end

# Index your tags/taggings
add_index :tags, :name
add_index :taggings, [:tag_id, :taggable_id, :taggable_type]
end

def self.down
drop_table "tags"
drop_table "taggings"
drop_table "products_tags
end

end


You really only need the first two tables to have it up and working.

Ruby Unit Testing

James Edward Gray shows how to apply unit tests to solving Ruby Quiz #84: Generating Pascal’s Triangle. In the process he also demonstrates a lot of features from the Ruby bundle and explains conventions and rationale behind them.

The Ruby Quiz screencast (42 MB) is almost an hour long, so you may want to grab a snack before you start it :)

sd.rb screencast

This is a a very good site from san diego ruby users group - http://podcast.sdruby.com/

Episodes includes

  • ActsAsTaggable Plugin
  • REST with Rails
  • Rails Authentication
  • Rails Deployment

Enjoy!