Archive for 四月, 2007

Apr
17

dennis写了几点rails实现验证码时需要注意的几点事项:

1.在windows上安装Rmagic,如果你是通过gem安装的,

require ’Rmagic’

要修改为:

require ’rubygems’
require ’Rmagick’

才能正确引入。
2.网上那个例子,画布是使用Rmagic内置的图像格式,Rmagic内置的图像格式还有:

gradient*

梯度,比如gradient:red-blue

granite

花岗石,比如: "granite:".

logo

logo型的图像. 如: "logo:",后面会多显示一个五角星^_^

netscape

非常漂亮的彩条。如: "netscape:"

null*

空白 使用方式: "null:"

rose

玫瑰 使用方式 : "rose:"

xc*

设置一个背景色,比如”xc:green”

一个修改的例子,在rails的models下存为noisy_image.rb,在Controller就可以这样调用NoisyImage.new(6) :

require ’rubygems’
require ’Rmagick’
class NoisyImage
  include Magick
  attr_reader :code, :code_image
  Jiggle = 15
  Wobble = 15
def initialize(len)
    chars = (’a’..’z').to_a - [’a',’e',’i',’o',’u']
    code_array=[]
1.upto(len) {code_array << chars[rand(chars.length)]}
    granite = Magick::ImageList.new(’xc:#EDF7E7′)
    canvas = Magick::ImageList.new
    canvas.new_image(32*len, 50, Magick::TextureFill.new(granite))
    text = Magick::Draw.new
    text.font_family = ‘times’
    text.pointsize = 40
    cur = 10
    code_array.each{|c|
      rand(10) > 5 ? rot=rand(Wobble):rot= -rand(Wobble)
      rand(10) > 5 ? weight = NormalWeight : weight = BoldWeight
      text.annotate(canvas,0,0,cur,30+rand(Jiggle),c){
        self.rotation=rot
        self.font_weight = weight
        self.fill = ‘green’
      }
      cur += 30
    }
    @code = code_array.to_s
    @code_image = canvas.to_blob{
      self.format=”JPG”
    }
  end
end

3.与rails应用的结合,和一般的验证码原理一样,将产生的随机数存储在session或者request范围内,提交的时候进行比较验证即可。比如产生图片的时候将随机字母存储在session[:code]中:

 session[:noisy_image] = NoisyImage.new(6)

 session[:code] = session[:noisy_image].code

验证的时候,比较提交的type_code与session[:code]即可,为了安全性考虑,最好还是不考虑使用客户端验证。

 unless session[:code]==params[:type_code]
      flash[:notice]=’验证码填写错误,请重新注册,谢谢!’
return redirect_to :action=>:new  
 end

在页面显示图片,类似servlet一样直接调用Controller的action:

def code_image
    image = session[:noisy_image].code_image
    send_data image, :type => ‘image/jpeg’, :disposition => ‘inline’
  end
<img height=’30′ src=”/test/code_image”>

Apr
17

因为硬盘坏了,换硬盘后安装了Windows XP SP2英文版,安装中文版QQ情况如下(截图为TM,QQ2007 Beta1一样不懂中文):

 

试用2005 English Version,有进步,居然可以看得见朋友列表上的中文,然而消息和群上的中文仍无法查看:

这让我比较惊讶,QQ竟不懂中文!

Apr
17

这么多年第一次遭遇了硬盘坏了,由于有部分代码没有调试完毕,关机睡觉前决定不提交代码到SVN,第二天早上起来打开电脑时,意外的找不到硬盘。

于是开机箱检测,听到硬盘在发出“噼、噼”的响声,经过一段时间的折腾,推断可能是硬盘坏了,磁头卡住了。可恨的Seagate.

因硬盘买回不足一月,送修,店员说只能换,不能修,数据找不回来。重写一天的代码不是很困难,但心理郁闷不不。现在丢失的代码已经基本重写完成,决定过段时间把开发计算机做成RAID,代码一定要在关机前进行Daily Build.

Apr
17

This is a temporary post that was not deleted. Please delete this manually. (0ae6c210-0617-4655-b05b-ffcf92754584)

Apr
11

使用acts_as_authenticated + authorization plugin 实现 Ruby on Rails 用户角色管理,step-by-step :

$ mysqladmin create auth_development
$ rails auth
$ ./script/plugin install http://svn.techno-weenie.net/projects/plugins/acts_as_authenticated 
$ ./script/plugin install http://svn.writertopia.com/svn/plugins/authorization
$ ./script/generate authenticated user account
$ ./script/generate role_model role
$ rake db:migrate
added following to my environment.rb:

AUTHORIZATION_MIXIN = “object roles”
DEFAULT_REDIRECTION_HASH = { :controller => ‘account’, :action =>
‘login’ }

added those to my user model:

  acts_as_authorized_user
  acts_as_authorizable

and this to test controller:

class TestController < ApplicationController
 permit "site_admin or moderator"
 
 def index
  render :text => “okok”
 end
end

$ ./script/server

Apr
11

 

4月8日参加了Kevin Yang组织的Rails源代码学习聚会,很高兴见到了慕名已久了人。

1)  如何获得Rails源代码
ruby 一键安装下载地址: ftp://ftp.kr.freebsd.org/pub/ruby/binaries/mswin32/
                                          http://rubyforge.org/frs/?group_id=167
radrails 下载地址: http://www.aptana.com/download_radrails.php
Rails SVN Trunk地址:

2) Rails内部模块的划分

    active_record
    active_support
    action_controller
    action_view
    action_mailer
    actioin_web_service
   
3) 如何debugRails源代码


4) ActiveRecord底层的数据库连接


5) ActiveRecord底层的row到object的map机理


6) Association是什么?


7) Rails是如何加载各种组件?

Apr
06

从Great News换到Google Reader已经有两个星期了,今天在读到周曙光的网络日志时出现问题,但左边的列表中明确的显示有(41)篇未读,于是Google一下,直接出来无法显示该页面错误,我知道他前几天去报道“钉子户”了,感觉可能被GWF了。

和他GTalk后,确认情况属实。

Apr
05

P1ay.cn launched at 2007-4-5 1:00 am, 做中国最时尚的网络生活杂志.

Apr
04

 

刚回到办公室后,收到了友派网寄来的《走过2006》主题杂志。看到这么些熟悉的面孔,印刷在精美的页面上,让你感受到:想当名人,有时候就这么简单。感谢林子,感谢这些真诚的朋友。