Rails 2.3.12:单独路由'GET / foo','PUT / foo'和'<any> / foo / bar'</any>

时间:2011-09-06 17:15:13

标签: ruby-on-rails

我有一个面向另一个应用程序的Rails 2.3.12应用程序。致电其他申请foo;发送到我的应用的所有URI都将以/foo开头。我的Rails应用程序需要处理这些情况:

  1. GET /foo - 我的应用直接处理(返回支持的列表)
  2. <anything-else> /foo - 返回401(即。GET是唯一受支持的HTTP动词)
  3. <anything> /foo/<anything> - 由我的应用处理,以传递到foo应用。
  4. 不幸的是,到目前为止,我的所有尝试都导致所有被案例1和2或案例3所采用。这就是我目前在routes.rb中所拥有的:

         map.root(:controller       => 'application',
                  :action           => 'render_401_unauthorised')
      map.connect('fooapp/*fooapp_ep_path',
                  :controller       => 'foo',
                  :action           => 'parsepath')
      map.connect('fooapp',
                  :controller       => 'other_controller',
                  :action           => 'index',
                  :conditions       => { :method => :get })
      map.connect('fooapp',
                  :controller       => 'application',
                  :action           => 'render_401_unauthorised')
    

    如果map.connect不为空,我认为如果我能确保第一个*fooap_ep_path 匹配,我的问题就会消失 - ie ,匹配正则表达式%r!/foo/.+!

    可以这样做吗?

    谢谢!

    已更新如果有任何方法可以指定必须为<非>为空,那么这可能会提供必要的粘合剂。

1 个答案:

答案 0 :(得分:0)

我使用#with_options按照

的方式解决了这个问题
map.with_options({}) do |ap,*args|
  ap.name1('fooapp',
           :controller    => 'can_do',
           :action        => 'index',
           :conditions    => { :method => :get }
          )
  ap.name2('fooapp',
           :controller    => 'can_do',
           :action        => 'render_403_forbidden'
          )
  ap.name3('fooapp/*fooapp_path',
           :controller    => 'other',
           :action        => 'do_it'
          )
end