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

如果我们的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的这个名为[color=blue]static_actions[/color]的插件,我们在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 !

项目地址:[url]http://github.com/ryanb/static_actions/tree/master[/url]
git拖下来:[color=darkblue]git clone git://github.com/ryanb/static_actions.git
[/color]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值