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

Sky diving! What it feels like.

Writing a new post after almost a year :) Penning it down here.. so that can relive that moment when i did the jump from 10000 feet up in...