从Rails1.2开始,我们不再需要通过map.connect来手动的配置REST路由,map.resources会帮我们搞定一切:
map.resources 'airports'
这句话将创建如下的路由规则:
* 针对/airports/ 的POST请求将被路由到create方法
* 针对/airports/1 的GET请求将被路由到show方法
* 针对/airports/1的PUT请求被路由到update方法
* 针对/airports/1 的DELETE请求被路由到destroy方法
* 针对/airports/ 的GET请求被路由到index方法
* 针对/airports/new 的GET请求被路由到new方法
* 针对/airports/1;edit 的GET请求被路由到edit方法
注意:最后一条逗号分隔的URL看起来很丑陋,但它们在Rails1.2.3中是合法的,不过不用苦恼,它们将在Rails2.0中被去除
上面这些信息希望能对你有帮助
关于url_for
复制内容到剪贴板
代码:
url_for :controller => 'posts', :action => 'recent' # => 'proto://host.com/posts/recent'
url_for :controller => 'posts', :action => 'index' # => 'proto://host.com/posts'
url_for :controller => 'posts', :action => 'show', :id => 10 # => 'proto://host.com/posts/show/10'API看看 应该就明白了