Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Path parsing in rails

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

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

like image 232
fl00r Avatar asked Apr 26 '10 15:04

fl00r


2 Answers

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

like image 77
Matt Simpson Avatar answered Oct 24 '22 00:10

Matt Simpson


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

like image 43
alex.zherdev Avatar answered Oct 24 '22 00:10

alex.zherdev