LoginSignup
21

More than 5 years have passed since last update.

posted at

URL(path)の文字列から Routing パラメーターの Hash に変換する

タイトルの通りです。一度調べて忘れたので、備忘録として。

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

対応する Route が無いと ActionController::RoutingError が出ます。
なお、その逆の働きをするのは

Rails.application.routes.generate_extras({:controller=>"users", :action=>"show", :id=>"1"})
=> ["/users/1", []]

です。
対応する Route が無いとこちらは ActionController::UrlGenerationError が出ます。

これで安心して忘れられます。

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
What you can do with signing up
21