{:c...">

Route analysis in rails

I am looking for a method to parse a route route as follows:

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

It should be opposite url_for

Update
I found out: What is the opposite of url_for in Rails? A function that takes a path and generates an interpreted route?

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

So now I need to convert posts_pathto "/ posts"

+5
source share
2 answers

Here is the method:

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

If you only have a line with your route (e.g. "posts_path"), then I assume that in the context you are using, you should be able to do

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

By the way, this also helped me :)

+5
source

Rails 3 :

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

ActionController::Routing::Routes.recognize_path

ActionController:: RoutingError: "/accounts/1

+12

All Articles