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

当使用名为routes的rails时,如何使参数成为可选的?

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

    我有一条指定路线:

    map.find '/find/:category/:state/:search_term/:permalink', :search_term=>nil, :controller=>'find', :action=>'show_match'
    

    以下URL与之匹配,工作正常:

    http://localhost:3000/find/cars/ca/TestSeachTerm/bumpedupphoto-test
    

    但是,如果我去掉最后第二个参数,即“testsearchterm”,那么路由将无法匹配,即使我在路由中有:search_term=>nil。

    http://localhost:3000/find/cars/ca//bumpedupphoto-test
    

    有人知道我做错了什么吗?正在努力解决这个问题几天了。

    谢谢!

    1 回复  |  直到 14 年前
        1
  •  1
  •   Zepplock    14 年前

    在当前路线后添加:

    map.find '/find/:category/:state/:permalink', :controller=>'find', :action=>'show_match'