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

rails中的路径分析

  •  5
  • fl00r  · 技术社区  · 14 年前

    我正在寻找解析路由路径的方法,如下所示:

    ActionController::Routing.new("post_path").parse
    #=> {:controller => "posts", :action => "index"}
    

    url_for


    What is the opposite of url_for in Rails? A function that takes a path and generates the interpreted route?

    ActionController::Routing::Routes.recognize_path("/posts")
    

    所以现在我需要转换 posts_path

    2 回复  |  直到 7 年前
        1
  •  5
  •   alex.zherdev    14 年前

    有这样一种方法:

    >> ActionController::Routing::Routes.recognize_path("/posts/")
    => {:action=>"index", :controller=>"posts"}
    

    如果你的路线只有一个字符串(比如 "posts_path"

    ActionController::Routing::Routes.recognize_path(send("posts_path".to_sym))
    

    顺便说一句,这对我也是一种教育:)

        2
  •  12
  •   Jeff    10 年前

    在Rails 3中,可以执行以下操作:

    Rails.application.routes.recognize_path "/accounts/1"
    # {:action=>"show", :controller=>"accounts", :id=>"1"}
    

    ActionController::Routing::Routes.recognize_path

    ActionController::RoutingError异常:没有与“/accounts”匹配的路由/1