• rails路由


    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)

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

  • 相关阅读:
    DOM对象模型接口规范中的四个基本接口
    js中几种实用的跨域方法原理详解(转)
    关于ie6/7下的z-index
    Mysql++学习(五)------专用SQL结构
    Mysql++学习(四)------模板查询
    Mysql++学习(三)------举个栗子
    Mysql++学习(二)------Mysql++简述
    Mysql++学习(一)------mysql编译安装
    epoll模型实例
    锁、页类型
  • 原文地址:https://www.cnblogs.com/juandx/p/3891280.html
Copyright © 2020-2023  润新知