代码之家  ›  专栏  ›  技术社区  ›  tipu

在ruby on rails中添加和访问控制器

  •  1
  • tipu  · 技术社区  · 14 年前

    如果我有一个控制器,我如何通过新添加的方法通过URL访问它?

    我之所以困惑是因为我有一条路,

    map.connect 'assignments/:external_id.:format', :controller => "assignments", :action => "show", :external_id => /\d{6}/
    

    似乎我无法访问 assignments 控制器,因为如果我这样做了

    mysite.com/assignments/other_method
    

    它将假定另一个方法是一个ID,我正在传入 show 控制器,如上面的路由条目中所指定。

    编辑:

    我在上面加了这个:

    map.connect 'assignments/send/', :controller => "assignments", :action => "send"
    

    现在得到这个错误:

    ArgumentError in AssignmentsController#show 
    

    的路线 assignments/send 是任何分配控制器的第一个声明

    2 回复  |  直到 14 年前
        1
  •  1
  •   Chris Barretto    14 年前

    你的路由表应该按这个顺序排列

    map.connect 'assignments/:external_id.:format', :controller => "assignments", :action => "show", :external_id => /\d{6}/
    
    map.connect 'assignments/send/', :controller => "assignments", :action => "send"
    

    以结束

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

    作为你最普遍的情况。

        2
  •  0
  •   dain    14 年前

    只需为第二种情况指定正确的路由模式,并确保记住从上到下计算映射(执行第一个匹配)。