draw_static
Cleaning up your static routes in rails
get 'home', to: 'pages#home'
get 'about', to: 'pages#about'
get 'contact-us', to: 'pages#contact_us'
Familiar?
Add
# Gemfile
gem 'draw_static'
To the Gemfile and then run bundle install, then add
# routes.rb
Rails.application.routes.draw do
draw_static :pages # Or which controller you are using for static routes
end
This set up will generate the static routes based on the controller actions. In this case we are generating the routes for the PagesController, if you want to do it for your PublicPagesController then you would pass: :public_pages
The controller action has to be the same name as the path. All non-word characters in the controller actions name are replaced by hyphens in the path.
e.g
def about_us
end
Will become
get 'about-us', to: 'pages#about_us'
Multiple controllers
You can specify as many controllers as you like.
# routes.rb
Rails.application.routes.draw do
draw_static :pages, :public, :sales # Or which controller you are using for static routes
end
Limits
# You can limit the routes that are generated by using except or only
draw_static :pages, only: [:home, :contact]
draw_static :pages, except: [:contact]
# also works for multiple controllers
draw_static :pages, :public, except: [:home, :contact]
# actions called either contact or home will be filtered from all controllers.
Enjoy!
Rails
draw_static is a gem for the rails framework, read more about the rails frame work here https://github.com/rails/rails