我的第二个插件,使用它,你可以很简单的集成站内用户聊天功能到你的Rails应用种。
实现时,参考了chatten项目,并使用它的部分源代码,因此,在这里要先感谢chatten项目的作者Jacob Pedersen。
安装
script/plugin install http://svn.elctech.com/svn/public/plugins/acts_as_chattable
Demo
svn co https://svn.elctech.com/svn/public/demo/acts_as_chattable
使用
首先,这个插件假设你有一个user model以及一个users controller,并且有一个current_user helper返回当前user,如果你是用RESTful Auth,它们应该已经在那里了,不过如果你还没有这些,那先创建它们,然后让我们开始:
1. 创建chat_message model:
script/generate chattable
2. 添加 acts_as_chattable 到 user model
class User
acts_as_chattable
...
def name
...
end
end
你最好为user model 定义一个name方法,否则所有用户的名字都将显示为’Unknown’.
3. 在users controller中包含ChatSystem module
class UsersController
include ChatSystem
...
end
4. 在视图中渲染聊天模块
<%= render_chat(friends, use_yui) %>
<%= buddy_list(friends, use_yui) %>
friends 应该是一个user model的数组,表示当前user 的好友
use_yui 表明是否使用YUI以获得一个更好的外观,默认为true。
你可以通过rdoc来获取这两个helpers的详细说明.
5. 这就搞定了,打开你的浏览器,开始享受聊天的乐趣吧!
最后,欢迎大家多提意见!
更新:特别感谢chm2920,帮我发现了IE7下工作不正常的Bug,新版本已修改,同事感谢我的同事Josh,新版本增加了一个generator,使用更加简单。

