六狼论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

新浪微博账号登陆

只需一步,快速开始

搜索
查看: 134|回复: 0

为非Restful的action准备的插件:static_actions

[复制链接]

升级  4.67%

64

主题

64

主题

64

主题

举人

Rank: 3Rank: 3

积分
214
 楼主| 发表于 2013-2-7 21:05:48 | 显示全部楼层 |阅读模式
如果我们的Rails应用里能够全部Restful,那很cool,但是现实不是这样。如 网站中的“关于我们”, “联系我们”, “帮助”等静态的controller/action

对于这样的静态资源,(":controller/:action"这种老形式的路由我们会删除掉)我们一般是在routes.rb中这样配置的:
  map.about_index 'about', :controller => 'about', :action => 'index'  map.about_privacy 'about/privacy', :controller => 'about', :action => 'privacy'  map.about_license 'about/license', :controller => 'about', :action => 'license'

或者在url后面加上.html的后缀:
  map.about_index_with_format 'about.:format', :controller => 'about', :action => 'index'  map.about_privacy_with_format 'about/privacy.:format', :controller => 'about', :action => 'privacy'  map.about_license_with_format 'about/license.:format', :controller => 'about', :action => 'license'
View页面中这样:
  <%= link_to "Privacy", about_privacy_path %>  <%= link_to "License PDF", about_license_with_format_path(:pdf) %>

现在有了RB的这个名为static_actions的插件,我们在routes.rb中这样配置就可以了:
map.static_actions :about, [:index, :privacy, :license]
这样会生成如/about/privacy  /about/license等这样的URL形式。
如果不想要前缀about,可以这样配置:
map.root_static_actions :about, [:index, :privacy, :license]

另外,如果about的controller中只有一个名为privacy的action,就不需要使用数组了:
map.static_action :about, :privacy


是不是很好的解决方案?Enjoy it !

项目地址:http://github.com/ryanb/static_actions/tree/master
git拖下来:git clone git://github.com/ryanb/static_actions.git
您需要登录后才可以回帖 登录 | 立即注册 新浪微博账号登陆

本版积分规则

快速回复 返回顶部 返回列表