by agate - Published: 2008-10-29 [3:39 下午] - Category: 程序编码

很早就在 "Web 开发大全 -- Ruby on Rails 版" 书中看到过 restful_authentication 这个鉴定插件了~ 但是一直都没使用过. 今天看到一个中文的 ROR 视频网站 -- rubycnrails.cn 上面用视频介绍了 restful_authentication 这个插件的使用(但是这位仁兄老是出错... 看着我好着急阿!) 呵呵! 于是我也试着使用了一番, 发现真的满好用的. 这里就记录一下, 顺便说一下需要注意的地方.

github 地址 http://github.com/technoweenie/restful-authentication/tree/master
(rubycnrails.cn 上面那个视频似乎是使用旧的版本... 大概不知道这个插件已经迁移到 github 上面了吧.)

安装:
$cd [your-rails-app]
$./script/plugin install git://github.com/technoweenie/restful-authentication.git

使用:
$./script/generate authenticated Model-Name [Controller-Name]
别忘了 rake 一下, 来建立你的数据库!

结果:
生成对应的 controller / model / view. 添加了对应的 routes: signup / login / logout. 当然, 还添加了

lib/authenticated_system.rb
lib/authenticated_test_helper.rb

这两个才是重要的插件功能! 其中 AuthenticatedSystem 中包含了诸如: logged_in?, current_kid 等重要方法! 到时候我们只需要在需要调用的 controller 中 include AuthenticatedSystem 就好了.

注意:
不要傻乎乎地学 README 中键入: ./script/generate authenticated user sessions 这个 s 最好不要!(不要说我违反了 RESTful 的理念, 我说的是最好不要!) 对于初学者来说先别加这个 s, 因为这个会带来很多配置上的模糊: 比如 routes.rb 中 resource 是定义为 'session', 但是设置具体命名路由的时候使用 controller 参数时又要设置为 'sessions'. 所以, 如果你对这里头的细节不是很清楚的话, 建议你改用单数作为这里控制器的名称:
$./script/generate authenticated user session 我建议这么写
当然! 你清除的话, 或者你不关心这个的话, 那还是写上那个 s 吧! 这样才够 RESTful!
其他很细致的内容还是看看源代码或者看看 Plugin 的 README 吧! 写得很不错!