Skip to content

Instantly share code, notes, and snippets.

@haileys
Last active August 29, 2015 14:22
Show Gist options
  • Save haileys/532bd15c1195aa7a9ebf to your computer and use it in GitHub Desktop.
Save haileys/532bd15c1195aa7a9ebf to your computer and use it in GitHub Desktop.
gem "rails", "3.2.21"
require "rails/all"
class MyApp < Rails::Application
end
def path_for(opts)
MyApp.routes.url_helpers.url_for(only_path: true, **opts)
rescue => e
e
end
MyApp.routes.draw do
get "/foo", to: "foo#index"
end
p path_for(controller: "foo", action: "index")
# => "/foo"
p path_for(controller: "foo", action: "doesnt_exist")
# => #<ActionController::RoutingError: No route matches {:controller=>"foo", :action=>"doesnt_exist"}>
MyApp.routes.draw do
get "/foo", to: "foo#index"
get "/nope", to: redirect("/")
end
p path_for(controller: "foo", action: "index")
# => "/foo"
p path_for(controller: "foo", action: "doesnt_exist")
# => "/nope?action=doesnt_exist&controller=foo"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment