Showing posts with label Route. Show all posts
Showing posts with label Route. Show all posts

Sunday, August 26, 2007

Rails Routes 详解

$>cd RubyOnRails/app/path
$>cd script/console
>>rs = ActionController::Routing::Routes
>>puts rs.routes
ANY /:controller/service.wsdl/ {:action=>"wsdl"}
ANY /:controller/:action/:id.:format/ {}
ANY /:controller/:action/:id/ {}
=>nil

修改config/routes.rb文件,添加一行
map.connect 'modules/controller/:action/*other', :controller => 'modules/controller'
回到console
>>rs.reload
>>puts rs.routes
ANY /:controller/service.wsdl/ {:action=>"wsdl"}
ANY /:controller/:action/:id.:format/ {}
ANY /:controller/:action/:id/ {}
ANY /modules/controller/:action/:other/ {:controller=>"modules/controller"}
=>nil
>>rs.recognize_path '/modules/controller/test/2?x=1'
=>{:action=>"test", :other=>["2?x=1"], :controller=>"modules/controller"}
>>rs.recognize_path '/test/action/1'
=>{:action=>"action", :id=>"1", :controller=>"test"}
>> rs.add_route('/x/y/z', {:controller => 'test', :action => 'index', :id => 0})=> #>>puts rs.routes
ANY /:controller/service.wsdl/ {:action=>"wsdl"}
ANY /:controller/:action/:id.:format/ {}
ANY /:controller/:action/:id/ {}
ANY /modules/controller/:action/:other/ {:controller=>"modules/controller"}
ANY /x/y/z/ {:action=>"index", :id=>0, :controller=>"test"}
=> nil
>> rs.draw do |map|
?> map.connect '/blog/:id', :controller => 'blog', :action => 'list'
>> end
=> [ActionController::Base, ActionView::Base]
>> puts rs.routes
ANY /blog/:id/ {:action=>"list", :controller=>"blog"}
=> nil
>> rs.recognize_path '/blog/1'
=> {:action=>"list", :id=>"1", :controller=>"blog"}
这里发现原来的routes全部不见了,只有这个新建的routes了,因为draw方法内部实现为:


def draw
clear!
yield Mapper.new(self)
named_routes.install
end

draw方法块里的map对象里method_missing方法就是用来实现具名路由named routes的。先看下源码如下:

def method_missing(route_name, *args, &proc)
super unless args.length >= 1 && proc.nil?
@set.add_named_route(route_name, *args)
end

上面对实例变量@set就是前面的rs对象,那么就看一下rs.add_named_route方法吧
>>rs.clear!
>>rs.add_named_route('index', 'user/index', :controller => 'user', :action => 'index')
puts rs.named_routes
#
=> nil
>> puts rs.routes
ANY /user/index/ {:action=>"index", :controller=>"user"}
=> nil
>> puts rs.named_routes.routes
indexANY /user/index/ {:action=>"index", :controller=>"user"}
=> nil
定义了具名路后就可以在controller/view中像url_for一样调用,只要在route name后面加上_url就可以,url_for在console里不能调试,可以用rs.generate(request)方法测试。
# In addition to providing url_for, named routes are also accessible after including UrlWriter.
url_for(:controller => 'signup', action => 'index', :token => token)
index_url(:id => 1)
#=> http://localhost/user/index?id=1
>> rs.generate(:controller => 'test', :action => 'index', :id => 3)
=> "/test/index/3"
另外在map这个对象里有个方法root可以给根目录定义路径,用法如下:

>> rs.draw do |map|
?> map.root :controller => 'test', :action => 'list'
>> end
ArgumentError: Illegal route: the :controller must be specified!
from /usr/local/lib/ruby/gems/1.8/gems/actionpack-1.13.3/lib/action_controller/routing.rb:965:in `build'
from /usr/local/lib/ruby/gems/1.8/gems/actionpack-1.13.3/lib/action_controller/routing.rb:1172:in `add_route'
from /usr/local/lib/ruby/gems/1.8/gems/actionpack-1.13.3/lib/action_controller/routing.rb:1178:in `add_named_route'
from /usr/local/lib/ruby/gems/1.8/gems/actionpack-1.13.3/lib/action_controller/routing.rb:997:in `root_without_deprecation'
from /usr/local/lib/ruby/gems/1.8/gems/activesupport-1.4.2/lib/active_support/deprecation.rb:94:in `root'
from (irb):60
from /usr/local/lib/ruby/gems/1.8/gems/actionpack-1.13.3/lib/action_controller/routing.rb:1139:in `draw'
from (irb):59


# Added deprecation notice for anyone who already added a named route called "root".
# It'll be used as a shortcut for map.connect '' in Rails 2.0.
# def root(*args, &proc)
# super unless args.length >= 1 && proc.nil?
# @set.add_named_route("root", *args)
# end
# deprecate :root => "(as the the label for a named route) will become a shortcut for map.connect '', so find another name"

Rails2.0才会引进此方法,所以Rails建议用户不要使用此具名路由,如下:
>> rs.draw do |map|
?> map.root '', :controller => 'test', :action => 'list'
>> end
=> [ActionController::Base, ActionView::Base]
>> puts rs.routes
ANY / {:action=>"list", :controller=>"test"}
=> nil
>> puts rs.named_routes.routes
rootANY / {:action=>"list", :controller=>"test"}
=> nil
这个写法其实就是做了个具名路由,而不是调用map.root方法,调用的是method_missing方法。

Monday, May 21, 2007

route命令详解

[localhost ~yu]# ping -c 4 192.168.1.1
PING 192.168.1.1 (192.168.1.1) 56(84) bytes of data.
64 bytes from 192.168.1.1: icmp_seq=1 ttl=64 time=0.683 ms
64 bytes from 192.168.1.1: icmp_seq=2 ttl=64 time=0.671 ms
64 bytes from 192.168.1.1: icmp_seq=3 ttl=64 time=0.683 ms
64 bytes from 192.168.1.1: icmp_seq=4 ttl=64 time=0.670 ms

--- 192.168.1.1 ping statistics ---
4 packets transmitted, 4 received, 0% packet loss, time 3000ms
rtt min/avg/max/mdev = 0.670/0.676/0.683/0.032 ms
说明局域网是正常的。

[localhost ~yu]# ping 202.101.63.200
connect: Network is unreachable
这是因为路由没有设置,在ifcfg-eth0文件里设置GATEWAY即可,或者是利用route命令,在命令行里添加一个路由或者默认路由。

[localhost ~yu]#route add default gw 192.168.1.1

route命令详解:
来源于iputils
语法 route [参数] 内部指令 [内部指令参数]
参数略
内部指令
del|add 删除增加路由
-net|-host 指定网段地址或主机地址
target 目的地址或者网络表示

内部指令参数:
netmask NM
gw IPADDR
metric M 设置路由表中的长度字段
mss M 设置TCP最大的区块长度为MB
refect 指定拒绝的路由。
def IF 指定该路由绑定特定的设备IF(如eth0)

范例:
通过eth0的设备增加一个路由到192.168.1.0(子网掩码为255.255.255.0)网段:
route add -net 192.168.1.0 netmask 255.255.255.0 dev eth0

要把目标地址是202.101.x.x的数据包通过202.101.62.254路由出去,使用:
route add –net 202.101.0.0 netmask 255.255.0.0 gw 202.101.62.254

删除添加默认路由:
route del default gw 192.168.0.1
route add default gw 192.168.1.1