Last active
August 29, 2015 14:22
-
-
Save haileys/532bd15c1195aa7a9ebf to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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