by agate - Published: 2008-12-25 [5:44 下午] - Category: 程序编码

默认情况下我们使用 rails 默认的 validates 来进行模型数据验证工作的时候常常会加上 message 参数来自定义我们的错误提示.

validates_presence_of :name, :message => 'is blank...'

出错的时候会提示

Name is blank...

但是要是我想显示

The name field is blank!

单靠改 message 的内容看来是不行的. 在 google 一番之后发现 ActiveRecord::Errors 这个类中有一个叫做 full_messages 的 helper function. 就是完成把出错的 field 的名字添加到 message 内容的前面的工作. 所以可以 override 一番. 就可以实现我们的功能了. 但是也许不小心会破坏过去的代码. 所以这里推荐一个插件( 由 rails 核心成员写的. )
Custom Error Message
其实十分简单, 它也是 override 了那个 helper function. 如果你不喜欢加插件的话自己在 lib 下面建立一个 ruby 文件就好了, 这里也顺便贴一下代码.

def full_messages
  full_messages = []

  @errors.each_key do |attr|
    @errors[attr].each do |msg|
    next if msg.nil?

      if attr == "base"
        full_messages << msg
      elsif msg =~ /^\^/
        full_messages << msg[1..-1]
      elsif msg.is_a? Proc
        full_messages << msg.call(@base)
      else
        full_messages << @base.class.human_attribute_name(attr) + " " + msg
      end
    end
  end

  return full_messages
end

使用方式:
在你需要自定义消息的前面加上 '^' 这个符号.(说白了就是很想正则告诉代码这是开始)

validates_presence_of :name, :message => '^The name field is blank!'

这样出错后就会显示:

The name field is blank!

Tags: [ , ] - Comments: View Comments
by agate - Published: 2008-12-25 [4:56 下午] - Category: 程序编码

javascript 中的 objcet 很特别, 说白了还是一个 hash, 今天突然需要便利所有 hash 中的 keys. 所以就查阅了一下其迭代的方法:

for (var key in hash) {
  // key is just what you need
  // you can get the value by hash['key']
}

更多 javascript 的 hash 实现方式可以参阅 Hash Tables in Javascript

Tags: [ ] - Comments: View Comments
by agate - Published: 2008-12-25 [7:59 上午] - Category: 程序编码

随着 IE 不断升级, 其也越来越规范化. 渐渐地和大多数浏览器也越来越表现一致化了~ 但是还是有一些不同. 这里就记录一下我所碰到的 IE 和 Firefox 解析 javascript 时候的语法区别:

1. 结尾逗号问题
[ 1, 2, 3 ]
{ a:1, b:2, c:3 }
一个 array 一个 hash 都是我们在 javascript 中常见的数据结构. 但是在 Firefox 中是可以在最后一个元素后面继续加入逗号的, 例如
[ 1, 2, 3, ]
{ a:1, b:2, c:3, }
但是 IE 就比较严格, 不可这么写, 必须去掉最后一个空格.
( PS 这点上我觉得 IE 比较规范... )

2. for 关键字
这里不是说循环, 而是说 hash 的赋值方式. 当我们的一个 hash 有一个 key 名字叫做 for 的话, 在 Firefox 中我们可以这么写:
hash.for = xxx OR xxx = hash.for OR hash['for'] = xxx OR xxx = hash['for']
但是很可惜, IE 中这样就会出错! 有可能其把 for 当作是关键字了或者什么其他特性( 不清楚 ), 总之 IE 中只能写成:
hash['for'] = xxx OR xxx = hash['for']

3. debugger 关键字
同 2 关键字. 一样的, debugger 在 ie 中也是一个关键字. 具体原因不知, 可能是俺装了 iedebugger 导致的. 总之最好改成其他名字, 或者使用 ['debugger']

[ updating ]

Tags: [ ] - Comments: View Comments
by agate - Published: 2008-12-23 [4:00 下午] - Category: 程序编码

在 javascript 中使用数组(哈希表)有很多小技巧(因为其是动态语言的缘故, 要比 java 灵活很多), 虽然基础了点, 但是还是想记录下来以备日后查阅:

基本特性
javascript 的数组不需要事先定义长度用时只要定义好数组的类型即可直接使用. 例如

var arr = []; // arr = new Array();
arr[1] = 111; // arr = [,1];
// 当然, hash 也是如此 hash = {}; 一样可以!

常用方法
push & pop
作为数组实现栈数据结构的方法组合. push 把给定参数加载数组最后, pop 则对应取出数组的最后一个元素.(改变数组本身)

push & shift
最为数组实现队数据结构的方法组合. shift 取出数组的第一个元素.(改变数组本身)

unshift
插入给定参数到数组之前. 比如 [2,3].unshift(0,1) => [0,1,2,3]

join
就是和 ruby 里头的 join 一样, 使用给定参数连接每个数组元素(方便处理最后一个连接, 而且效率很快). 例如

['CCTV', 'SMG', 'MTV'].join(', ') => 'CCTV, SMG, MTV'

sort & reverse
对应的两种排序方式, sort 为从小到达, reverse 为从大到小.

slice & splice
对应的两种数组截取方式. slice 只是截取出数组的某个区间段并输出(其不改变数组本身), splice 截取了某个区间段并替换成给定参数(没有给定参数就等于直接删除这个区间, 当然会改变数组本身). 例如

arr      = [1,2,3,4,5]
arr_temp = arr.slice(2,4) // arr_temp = [3,4] & arr = [1,2,3,4,5]
arr.splice(2,4,0) // arr = [1,2,0]
// 注意了 slice(m,n) 是取出 [m,n)
// splice(m,n,...) 是替换 [m,n]

总结
其实呢~ javascript 是一个很奇特的脚本语言, 其中所有的东西都是变量/hash/数组, 而且只要声明了类型就可以直接使用, 不用担心什么越界之类的.

Tags: [ ] - Comments: View Comments
by agate - Published: 2008-12-22 [4:24 下午] - Category: 程序编码

Eval
以 string 的格式执行 ruby 表达式. 比如
eval "str = %{helloworld}"
就会创建一个 str 变量, 其值为 "helloworld"

obj.send
调用 obj 中的方法以 symbol 的形式. 比如
class User
  def say(str)
    "user says: #{str}"
  end
end
u = User.new
u.send :say, 'i am a boy~' #=> "user says: i am a boy~"

如果这个 obj 中有方法覆盖了 send 方法, 那么我们可以使用 __send__ 来代替 send 方法

Tags: [ ] - Comments: View Comments
by agate - Published: 2008-12-13 [12:45 下午] - Category: 历程, 日志

人必须有点强迫症, 如果不是一开始彻底全部使用英文操作系统, 看英文书籍, 听英文技术视频... 也许也没有现在的感受~

很谢谢那些被我白痴英语不断骚扰的好心黑客们~ 很谢谢 team 里的各个兄弟们! 谢谢分享这么多易读有趣能给人启发的英文文章!

所以, 渐渐地, 我发现写英文不累了, 虽然还是很跛脚...
所以, 渐渐地, 我发现听对话不累了, 虽然还是半猜测...
所以, 渐渐地, 也许我会敢开口说了, 虽然还要点时间...
所以, 渐渐地, 我会习惯使用困扰我这么多年的英文.

祝福我身边的朋友, 同事, 祝福我的家人!

Tags: [ ] - Comments: View Comments
by agate - Published: 2008-12-07 [8:18 上午] - Category: 开发环境

i feel uncomfortable while i using vim and having to move way up to reach Escape key. so i swaped the Escape and CapsLock keys by under steps:

first, open your terminal. and type:
$vim ~/.xmodmap

second, add under statements into ./xmodmap file.
remove Lock = Caps_Lock
keysym Caps_Lock = Escape
keysym Escape = Caps_Lock
add Lock = Caps_Lock

last, restart your X env.
(maybe you can use CTRL + ALT + BackSpace)

after these configurations, i found it's easier than move way up to reach Esc while using vim.

ps. thanks forrest, thanks leon! i found i fell in love with vim!!! it's really powerful!

Tags: [ , , ] - Comments: View Comments
by agate - Published: 2008-12-04 [4:57 下午] - Category: 软件使用

last time i introduce a on-line code hoster Pastie --- you can share you code with your friends.

today, i found that github has a same function.
gist.github.com

http://gist.github.com/xxxx is your code page.
http://gist.github.com/xxxx.txt is your code text.

Tags: [ , ] - Comments: View Comments