Path parsing in rails

前端 未结 2 992
灰色年华
灰色年华 2021-01-12 19:08

I am looking for method for parsing route path like this:

ActionController::Routing.new(\"post_path\").parse
#=> {:controller => \"posts\", :action =&g         


        
相关标签:
2条回答
  • 2021-01-12 19:51

    In Rails 3 you can do the following:

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

    Using ActionController::Routing::Routes.recognize_path kept throwing

    ActionController::RoutingError Exception: No route matches "/accounts/1

    0 讨论(0)
  • 2021-01-12 20:07

    There's this method:

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

    If you only have a string with your route (like "posts_path"), then I guess in the context you're using this you should be able to do

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

    btw this was educating for me too :)

    0 讨论(0)
提交回复
热议问题