Monday, January 5, 2015

How to access rails application routes in rails models

With the newer version of rails, application itself is an engine which is the initialized format of abstract class Rails::Application and to access routes of any (host) engine the code is

SomeRailsApplicationName::Application.routes.url_helpers.named_route_path(parameter: 'something')

and for a (mountable) engine the code is

SomeMountableEngineName::Engine.routes.url_helpers.named_route_path(parameter: 'something'


rails console > Helloworld::Application.routes.url_helpers.about_path
 => "/about"

rails console > BambooUser::Engine.routes.url_helpers.login_path
 => "/users/login"

No comments:

Post a Comment

The baffling IQ test

Getting 100 per cent means you “have a passion for perfection and you like to challenge yourself. You excel in finding problems and solutio...