How do i cache a routes in to a variable, with lambda, but how to create it outside of the routes block?
somefile.rb that is loaded before the routes.rb block gets called:
x = lambda do
namespace :test do
root to: 'application#index'
get 'page/:page', to: 'pages#show', as: :page
end
end
routes.rb:
Rails.application.routes.draw do
x.call if yep
end
Code like this doesn’t work because of some DSL class loading error. I don’t really understand how the scope works inside of blocks.
You can simply pass your lambda to the
drawmethod directly:In this example, I’m defining the lambda in the same file (
config/routes.rb), but you could put it in an initializer or a library file or wherever you like: