Quickly test a Rails routing table from IRB

:: ruby, ruby-on-rails

By: David

Here is a reminder of two quick ways to check your routes work, directly in IRB. Firstly, what path can you generate from your helpers?

1
2
3
4
irb(main):001:0> app.url_for [Client.last, :manage, Project.last]
  Client Load (2.0ms)  SELECT "clients".* FROM "clients" WHERE "clients"."deleted_at" IS NULL ORDER BY "clients"."id" DESC LIMIT $1  [["LIMIT", 1]]
  Project Load (2.0ms)  SELECT "media_stores".* FROM "media_stores" WHERE "media_stores"."type" = $1 AND "media_stores"."deleted_at" IS NULL ORDER BY "media_stores"."id" DESC LIMIT $2  [["type", "Project"], ["LIMIT", 1]]
=> "http://www.example.com/2-gelato-4-u/manage/projects/7"

Secondly, how is my path being interpreted?

1
2
3
4
5
6
7
8
irb(main):019:0> r = Rails.application.routes
=> #<ActionDispatch::Routing::RouteSet:0x000000010eae3030>
irb(main):025:0> r.recognize_path "/1/manage/projects"
=> {:controller=>"clients/manage/projects", :action=>"index", :client_id=>"1"}
irb(main):026:0> r.recognize_path "/1/manage/project"
=> No route matches "/1/manage/project" (ActionController::RoutingError)
irb(main):027:0> r.recognize_path "/1/manage/projects/1"
=> {:controller=>"clients/manage/projects", :action=>"show", :client_id=>"1", :id=>"1"}