码迷,mamicode.com
首页 > 其他好文 > 详细

rails路由

时间:2014-08-05 00:31:58      阅读:252      评论:0      收藏:0      [点我收藏+]

标签:blog   http   使用   os   io   for   art   ar   

web敏捷开发 p317

 

depot > ruby script/console

>>rs = ActionController::Routing::routes

可以简单测试

比如

>>puts rs.routes

>>rs.recognize_path "/store"

>>rs.generate :controller => "store",id => 123

 

connect 定义路由规则

url_for 可以生成url,但只能在控制器里使用

 

 

具名路由

map.index "blog/", :controller => "blog" , :action => "index"

这样就可以使用index_url就可以得到http://pragprog.com/blog

如果使用index_path就可以得到/blog,即没有协议、主机地址、端口

 

 

有根路由

在不同子域下访问相同的内容

http://megablogworld.com/dava/blog

http://megablogworld.com/wen/blog

http://megablogworld.com/wang/blog

dava、wen、wang是不同的应用程序实例

设置RAILS_RELATIVE_URL_ROOT

 

 

 

资源

map.resources :articles

比如文章目录是一组资源,一篇文章是一项资源

 

post get put delete是不同的http请求

 

depot > rake routes可以查看资源映射的路由

可以看到7个action       index new edit...

 

:collection 一组资源 get /articles/recent

:member 一项资源 put /articles/1/embargo

:new 新建资源 post /aritcles/new/shortform

 

 

嵌套资源

文章的评论是嵌套在文章下的资源

map.resources :articles do |article|

  article.resources :comments

  # ...

end

 

 

浅路由嵌套

 

format

html请求对应html

xml请求对应xml

 map.connect ‘:controller/:action/:id.:format‘ 

class ProductsController < ApplicationController
# GET /products
# GET /products.xml
def index
@products = Product.find(:all)

respond_to do |format|
format.html # index.html.erb
format.xml { render :xml => @products }
end
end

 

路由测试

assert_generates(path, options , defaults={}, extras={}, message=nil)

 

 

这些只是摘要,具体看书吧

rails路由,布布扣,bubuko.com

rails路由

标签:blog   http   使用   os   io   for   art   ar   

原文地址:http://www.cnblogs.com/juandx/p/3891280.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!