views:

46

answers:

1

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

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

It should be opposite to url_for

Upd
I've found out: http://stackoverflow.com/questions/2222522/what-is-the-opposite-of-url-for-in-rails-a-function-that-takes-a-path-and-genera

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

So now I need to convert posts_path into "/posts"

+1  A: 

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 :)

neutrino
ah you've done most of the job yourself :)
neutrino
Thx :) so what is `send(:post_path)`?
fl00r
it's how you call methods with an arbitrary name in ruby :) in this case, if you have a string like `"posts_path"`, and you want to get the value of `posts_path` method instead, then you just do `send("posts_path")` (you don't even need to convert to symbol). it's a core concept in ruby, you'd better be familiar with that :)
neutrino
ok, thanks, I've got it. I know how `send` works (like eval in this case). But problem was in console (I've just tried `ActionController::Routing::Routes.recognize_path(send("post_path".to_sym))`) so I've got confused because that failed :)
fl00r