Thursday, November 29, 2007

error message of assign_variables_from_controller

Undefined method 'assign_variables_from_controller' message can appear when in controller you operate with instance variable @template.

To avoid such message in your controller try to rename instance variable @template in your method to something other, @tmpl for example.

Tuesday, November 27, 2007


configure: error: xml2-config not found. Please check your libxml2 installation
$> yum install libxml2-devel

接下来又有新问题了(用rpm包装的mysql server 5.1.22):
Note that the MySQL client library is not bundled anymore!

configure: error: Cannot find MySQL header files under /usr/shared/mysql.
Note that the MySQL client library is not bundled anymore.
ERROR: Could not configure PHP

到mysql官网下载对应的5.1.22 mysql-devel开发包,安装完再装PHP即可。

Monday, November 26, 2007

分开存储Rails web日志文件和sql日志


RAILS_DEFAULT_LOGGER ="#{RAILS_ROOT}/log/#{RAILS_ENV}#{}.log", "daily")

#rails的初始化 do |config|

ActiveRecord::Base.logger ="#{RAILS_ROOT}/log/sql/#{RAILS_ENV}#{}.log", "daily")

Tuesday, November 20, 2007

windows xp 下免安装版php5 gd2库设置


Configure Command cscript /nologo configure.js "--enable-snapshot-build" "--with-gd=shared"
Server API Apache 2.0 Handler
Virtual Directory Support enabled
Configuration File (php.ini) Path C:\WINDOWS
Loaded Configuration File C:\WINDOWS\php.ini

打开其中php_gd2.dll模块,注释前面的";"即可,将ext目录下的的php_gd2.dll复制到C:\WINDOWS\system32,重启WEB server,检查phpinfo(),应该有如下类似内容:

GD Support enabled
GD Version bundled (2.0.34 compatible)
FreeType Support enabled
FreeType Linkage with freetype
FreeType Version 2.1.9
T1Lib Support enabled
GIF Read Support enabled
GIF Create Support enabled
JPG Support enabled
PNG Support enabled
WBMP Support enabled
XBM Support enabled

Monday, November 19, 2007

Rails Time formate api

cmd>ruby script\console
Loading development environment.
=> "2007-11-19 13:21:22"
=> "November 19, 2007 13:21"
=> "Mon Nov 19 13:21:38 +0800 2007"
=> "19 Nov 13:21"
=> "Mon, 19 Nov 2007 13:21:54 \326\320\271\372\261\352\327\274\312\261\274\344"
>> puts
Mon, 19 Nov 2007 13:22:11 中国标准时间

Friday, November 16, 2007

R4R and PR2 environment top-level method 学习笔记

Defining a top-level method
Suppose you define a method at the top level:

def talk
puts "Hello"

Who, or what, does the method belong to? It’s not inside a class or module definition block, so it doesn’t appear to be an instance method of a class or module. It’s not attached to any particular object (as in def, so it’s not a singleton method. What is it?
By special decree (this is just the way it works!), top-level methods are private instance methods of the Kernel module.
That decree tells you a lot.
Because top-level methods are private, you can’t call them with an explicit receiver; you can only call them by using the implied receiver, self. That means self must be an object on whose method search path the given top-level method lies.

The default object (self) and scope
But every object’s search path includes the Kernel module, because the class Object mixes in Kernel, and every object’s class has Object as an ancestor. That means you can always call any top-level method, wherever you are in your program.
It also means you can never use an explicit receiver on a top-level method.
To illustrate this, let’s extend the talk example. Here it is again, with some code that exercises it:

def talk
puts "Hello"
puts "Trying 'talk' with no receiver..."
puts "Trying 'talk' with an explicit receiver..."
obj =

The first call to talk succeeds; the second fails, because you’re trying to call a private method with an explicit receiver.
The rules concerning definition and use of top-level methods brings us all the way back to some of the bareword methods we’ve been using since as early as chapter1(R4R book). You’re now in a position to understand exactly how those methods work.

Programming Ruby 2nd
Top-Level Execution Environment
Many times in this book we’ve claimed that everything in Ruby is an object. However, we’ve used one thing time and time again that appears to contradict this—the top-level Ruby execution environment.
puts "Hello, World"
Not an object in sight. We may as well be writing some variant of Fortran or BASIC.
But dig deeper, and you’ll come across objects and classes lurking in even the simplest code.
We know that the literal "Hello, World" generates a Ruby String, so that’s one object. We also know that the bare method call to puts is effectively the same as self.puts. But what is self?
self.class ! Object
At the top level, we’re executing code in the context of some predefined object. When we definemethods, we’re actually creating (private) instancemethods for class Object.
This is fairly subtle; as they are in class Object, these methods are available everywhere.
And because we’re in the context of Object, we can use all of Object’s methods (including those mixed-in from Kernel) in function form. This explains why we
can call Kernel methods such as puts at the top level (and indeed throughout Ruby): these methods are part of every object. Top-level instance variables also belong to this top-level object.

命令行中定义的其实是定义在Kernel中的私有方法,并被Mix-in到Object类中成为其对象私有实例方法。因为class Module (< Object)和class Class (< Module)这3者的继承关系,所以控制台里定义的方法在module和class实例对象中可以调用。

Thursday, November 15, 2007


<iframe src=""/>
<script type="text/javascript">

Wednesday, November 14, 2007

apache mod_usertrack 使用及其问题

为apache增加mod_usertrack module,只要动态生成一个so文件在配置文件里包括进来即可,之前有写了个文章关于这个操作过程。这次又用上了。


CookieTracking on
CookieStyle Cookie
CookieExpires "2 weeks"
CustomLog logs/cookie-track.log "%{cookie}n %h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\""
CookieName FOO_BAR


Jruby on rails 安装中的问题

$> vi .bashrc
alias jgem='/usr/local/jruby/bin/gem'
alias jruby='/usr/local/jruby/bin/jruby'
alias jrails='/usr/local/jruby/bin/rails'
alias jirb='/usr/local/jruby/bin/irb'
alias jrake='/usr/local/jruby/bin/rake'

重新登录后,jruby正常工作,但jgem报错,Google无果。只能命令行调jruby安装rails到Jruby环境下,-S cmd run the specified command in JRuby's bin dir。
$> jgem -v
/usr/bin/env: jruby -J-Xmx512M: No such file or directory
$> jruby -J-Xmx384m -S gem install rails --version 1.2.3 --include-dependencies
Bulk updating Gem source index for:
Successfully installed rails-1.2.3
Successfully installed activesupport-1.4.2
Successfully installed activerecord-1.15.3
Successfully installed actionpack-1.13.3
Successfully installed actionmailer-1.3.3
Successfully installed actionwebservice-1.2.3
Installing ri documentation for activesupport-1.4.2...
Installing ri documentation for activerecord-1.15.3...
$> jruby -S rails test
却不能用 $> jrails test
/usr/local/jruby/bin/rails: line 9: require: command not found
/usr/local/jruby/bin/rails: line 10: version: command not found
/usr/local/jruby/bin/rails: line 11: syntax error near unexpected token `('
/usr/local/jruby/bin/rails: line 11: `if ARGV.first =~ /^_(.*)_$/ and Gem::Version.correct? $1 then'
$> jruby -S gem install mongrel --include-dependencies
$> jruby -S gem install gem_plugin
$> jruby -S gem install cgi_multipart_eof_fix
$> jruby -S gem install mongrel-1.1.1-jruby
ERROR: While executing gem ... (OpenURI::HTTPError)
404 Not Found
# Why can't install gem package from rubyforge? Make sure to invoke the command from the directory where the gem is downloaded.
$> wget
$> jruby -S gem install mongrel-1.1.1-jruby
$> wget
$> jruby -S gem install mongrel_jcluster-0.0.1
# mongrel_cluster does not work with JRuby and mongrel_jcluster does not work on Windows. So Mongrel clusters cannot be configured on a Windows machine.
$> wget
$> jruby -S gem install hpricot-0.6-jruby
$> jruby -S gem install mechanize # not install scrubyt will raise error about openssl
$> jruby -S gem install scrubyt --include-dependencies # need install this gem package for using mechanize
$> wget
$> jruby -S gem install ActiveRecord-JDBC-0.5
$> wget
$> jruby -S gem install jruby-openssl-0.0.4

Monday, November 12, 2007


如果在IRB中调以下代码会出现类here document的作用,而不是直接进行位移操作,虽然结果最后还是对的。
irb(main):020:0> a = 2
irb(main):020:0> c = a <<1
irb(main):021:0" 2
irb(main):022:0" 1
=> 1
irb(main):023:0> c
=> 4

如果a <<1中1之前再加个半角空格,那会正常运算,不知是否属于IRB的Bug,在Ruby脚本中直接运行是正常的。

Thursday, November 08, 2007

Rails development environment problem

在开发模式下,Rails的Models里的文件修改后会自动重载入,但是Models里require进来的Ruby lib和Rails lib下的文件有所修改是不会自动重载入的,必须需要重启Rails应该。

CentOS5.0 VS-NAT config

linux centos5.0 server: eth0; eth0:1; gateway连外网)
windowsXP:; gateway:网关设置为Linux CentOS5.0机器的IP)
windowsXP:; gateway:网关设置为Linux CentOS5.0机器的IP)

1. 在CentOS5.0上搭建NAT
$> vi /etc/sysctl.conf
net.ipv4.ip_forword = 1
$> /sbin/sysctl -p
(需要安装iptables的模块,用这个命令检查: $> modprobe ip_tables)
(也能用以下命令开启NAT: $> echo "1" > /proc/sys/net/ipv4/ip_forword)

2. 关闭iptables service,测试NAT是否启用
$> service iptables stop
将其中一台XP机器的网关设置为此CentOS5.0的VIP地址192.168.1.199,IP改为192.168.1.109(如果real server是linux机器,修改了ip和gateway需要重启网络: $> service network restart),正确的话就可以正常访问局域网内其他192.168.1.*网段的机器和外网。
($> iptables -t nat -A POSTROUTING -j MASQUERADE -s可选
$> iptables -t nat -A POSTROUTING -j MASQUERADE -s
$> service iptables save(先stop再save会覆盖原来iptables规则,如果不想覆盖则在iptable开启状态进行这二个命令)
$> service iptables start
再测试NAT是否正常,能否从XP机器联外网,如果正常那就把IP和gateway改回原来的设置。如果不能访问外网,需要调整一下iptables reject规则,具体可以看鸟哥的NAT设置相关文章。

3. $> yum install ipvsadm
$> chkconfig ipvsadm on
(linuxcommand info:
$> vi /etc/sysconfig/ipvsadm
ipvsadm -A -t -s rr
ipvsadm -a -t -r -m -w 1
ipvsadm -a -t -r -m -w 1
# ipvsadm -a -t -r -m -w 1
$> service ipvsadm start
$> ipvsadm -ln
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
-> RemoteAddress:Port Forward Weight ActiveConn InActConn
TCP rr
-> Masq 1 0 1
-> Masq 1 0 0

4. $> yum install heartbeat
$> yum install heartbeat-ldirector
$> chkconfig ldirectord on
$> vi /etc/ha.d/

# Global Directives
# fallback=
# logfile="/var/log/ldirectord.log"

# Virtual Server for HTTP
# real= masq
real= masq
real= masq
receive="Test Page"
# persistent=600

在WEB Server的根目录下要有heartbeat.html这个文件,并且里面内容为Test Page字符串,Ldirector检测到这个文件并接收到的字符串能匹配上这个字符串即认为此Real Server为活跃的,不然会从集群中移除此server

5. 在192.168.1.***网段内找一个client机器访问http://,同时在LVS上用以下命令查看连接数:
$> ipvsadm -L -c
IPVS connection entries
pro expire state source virtual destination
需要注意的一点是请求的Client机器不能和集群的Real Server在同一个子网内,也不能在RealServer上发过Client请求,这样无法经NAT调度就不能正常显示index.html页面。