Created
July 26, 2012 10:22
-
-
Save solnic/3181379 to your computer and use it in GitHub Desktop.
Determine current route name in Rails
This file contains 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
def route_name | |
Rails.application.routes.recognize(request) { |route, _| route.name }.flatten.last.name.to_sym | |
end |
Hakon
commented
Jul 26, 2012
@Hakon thanks man. I updated the gist :)
Or you could just do an explicit return from inside the block
def route_name
Rails.application.routes.router.recognize(request) do |route, _|
return route.name.to_sym
end
end
In ruby you do not need use return. Last line is return line ;)
explicit return is needed because
Rails.application.routes.router.recognize(request)
returns a set of nested arrays and not the value of the block. It is also the last executed line in the route_name method.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment