如果我们的Rails应用里能够全部Restful,那很cool,但是现实不是这样。如 网站中的“关于我们”, “联系我们”, “帮助”等静态的controller/action
对于这样的静态资源,(":controller/:action"这种老形式的路由我们会删除掉)我们一般是在routes.rb中这样配置的:
或者在url后面加上.html的后缀:
View页面中这样:
现在有了RB的这个名为[color=blue]static_actions[/color]的插件,我们在routes.rb中这样配置就可以了:
这样会生成如/about/privacy /about/license等这样的URL形式。
如果不想要前缀about,可以这样配置:
另外,如果about的controller中只有一个名为privacy的action,就不需要使用数组了:
是不是很好的解决方案?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]
对于这样的静态资源,(":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]