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

PHP/Mysql新版本安装问题

php安装了不知道多少次,经常会碰到一些安装问题,这次装5.2.5碰到2个错误如下:
configure: error: xml2-config not found. Please check your libxml2 installation
在CentOS5.0中执行以下命令:
$> 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_project/log/目录下新建sql文件夹,sql文件夹用来保存sql日志
接着,修改rails_project/config/environment.rb代码,需注意代码顺序。

#web访问日志
RAILS_DEFAULT_LOGGER = Logger.new("#{RAILS_ROOT}/log/#{RAILS_ENV}#{Date.today.to_s}.log", "daily")

#rails的初始化
Rails::Initializer.run do |config|
#...
end

#sql日志
ActiveRecord::Base.logger = Logger.new("#{RAILS_ROOT}/log/sql/#{RAILS_ENV}#{Date.today.to_s}.log", "daily")

Tuesday, November 20, 2007

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

phpinfo()里的一段信息如下,PHP加载的配置文件默认放到C:\WINDOWS\php.ini下。


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
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.
>> Time.now.to_s(:db)
=> "2007-11-19 13:21:22"
>> Time.now.to_s(:long)
=> "November 19, 2007 13:21"
>> Time.now.to_s(:stamp)
=> "Mon Nov 19 13:21:38 +0800 2007"
>> Time.now.to_s(:short)
=> "19 Nov 13:21"
>> Time.now.to_s(:rfc822)
=> "Mon, 19 Nov 2007 13:21:54 \326\320\271\372\261\352\327\274\312\261\274\344"
>> puts Time.now.to_s(:rfc822)
Mon, 19 Nov 2007 13:22:11 中国标准时间

Friday, November 16, 2007

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

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


def talk
puts "Hello"
end

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 obj.talk), 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"
end
puts "Trying 'talk' with no receiver..."
talk
puts "Trying 'talk' with an explicit receiver..."
obj = Object.new
obj.talk

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标签关闭不正确造成的问题

<iframe src="http://www.baidu.com"/>
因为iframe标签关闭不正确,后面的Javascript和此文本内容在FF/IE上(其他浏览器未测试)都不会被执行,HTML输出流截止于iframe。
<script type="text/javascript">
alert('test');
</script>

Wednesday, November 14, 2007

apache mod_usertrack 使用及其问题

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

在虚拟主机的配置里再加上以下配置,其中具体的参数含义可参考此链接:http://httpd.apache.org/docs/1.3/mod/mod_usertrack.html


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

重启apache即可。
这个模块在Mozilla/Firefox中工作一切正常,而且可以做第3方的Cookie跟踪。
但是在IE(仅测试了IE6和IE7)中这个Cookie工作机制却类似于phpsessionid的工作机制,关闭IE重开之后却重新生了一个Cookie,如果不关则能正常记录用户的clickstream。但是从客户端查看HttpResponse则看得出来服务器端返回来的是正常的
请求,是让IE写个2周的Cookie,不过事实IE并不这样工作。至于用作第3方Cookie跟踪就更是不行,受P3P策略限制。

Jruby on rails 安装中的问题

在安装好jdk和jruby后并测试jruby正常工作后,设置了几个命令别名,以防跟原来系统装的ruby命令冲突,ruby下的irb/rails/gem/ruby等命令是包含到系统中的。Jruby要以示区别:
$> 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: http://gems.rubyforge.org
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...
....
新建Jrails项目如下:
$> 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'
哪位仁兄如果看到此问题并有解决方案请跟贴指点,谢谢。
另附mongrel+jruby的windows安装过程:
$> 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 http://rubyforge.org/frs/download.php/27884/mongrel-1.1.1-jruby.gem
$> jruby -S gem install mongrel-1.1.1-jruby
$> wget http://rubyforge.org/frs/download.php/20455/mongrel_jcluster-0.0.1.gem
$> 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 http://rubyforge.org/frs/download.php/21765/hpricot-0.6-jruby.gem
$> 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 http://rubyforge.org/frs/download.php/24515/ActiveRecord-JDBC-0.5.gem
$> jruby -S gem install ActiveRecord-JDBC-0.5
$> wget http://rubyforge.org/frs/download.php/23995/jruby-openssl-0.0.4.gem
$> jruby -S gem install jruby-openssl-0.0.4

Monday, November 12, 2007

irb中<<左位移操作符问题

如果在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

参考http://zh.linuxvirtualserver.org/node/26,环境如下:
linux centos5.0 server: eth0 192.168.0.199(RIP); eth0:1 192.168.1.199(VIP); gateway 192.168.1.1(连外网)
windowsXP: 192.168.0.109(RIP); gateway: 192.168.0.199(网关设置为Linux CentOS5.0机器的IP)
windowsXP: 192.168.0.112(RIP); gateway: 192.168.0.199(网关设置为Linux CentOS5.0机器的IP)
XP机器都有开80端口WEB服务,linux机器上80端口的服务不一定要开。

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.*网段的机器和外网。
测试NAT通过后:
($> iptables -t nat -A POSTROUTING -j MASQUERADE -s 192.168.1.0/24)可选
$> iptables -t nat -A POSTROUTING -j MASQUERADE -s 192.168.0.0/24
$> 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: http://www.linuxcommand.org/man_pages/ipvsadm8.html)
$> vi /etc/sysconfig/ipvsadm
ipvsadm -A -t 192.168.1.199:80 -s rr
ipvsadm -a -t 192.168.1.199:80 -r 192.168.0.109:80 -m -w 1
ipvsadm -a -t 192.168.1.199:80 -r 192.168.0.112:80 -m -w 1
# ipvsadm -a -t 192.168.1.199:80 -r 192.168.0.199:80 -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 192.168.1.199:80 rr
-> 192.168.0.112:80 Masq 1 0 1
-> 192.168.0.109:80 Masq 1 0 0

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


# Global Directives
checktimeout=10
checkinterval=2
# fallback=127.0.0.1:80
autoreload=no
# logfile="/var/log/ldirectord.log"
logfile="local0"
quiescent=yes

# Virtual Server for HTTP
virtual=192.168.1.199:80
fallback=127.0.0.1:80
# real=192.168.0.199:80 masq
real=192.168.0.109:80 masq
real=192.168.1.112:80 masq
service=http
request="heartbeat.html"
receive="Test Page"
scheduler=rr
# persistent=600
protocol=tcp
checktype=negotiate

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

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