I'm trying to think of a better way to write these routes in my routes.rb file:
Rails.application.routes.draw do
root 'home#index'
s = /section1|section2|section3|section4/
y = /\d{4}/
m = /\d{1,2}/
resources :articles, except: [:index, :show]
# The slug will replace the default params[:id] of the following resources:
resources :articles, only: [:index, :show], path: '/:section/:year/:month', as: :my_articles, constraints: {:section => s, :year => y, month: m}
get ':section/:year/:month', to: 'articles#by_month', as: :month, constraints: {section: s, year: y, month: m}
get ':section/:year', to: 'articles#by_year', as: :year, constraints: {section: s, year: y}
get ':section', to: 'articles#by_section', as: :section, constraints: {section: s}
end
This enables me to search articles by section, by year+section, by month+section+year. It also lets me add a nice slug to URLs.
One of the problems is that the constraints are common among all these routes. I'm wondering if there's a more clever way to go about writing this. Someone suggested I use concerns but I haven't figured out a way to do that.
Not a game changer, but I'm curious as to how I can write this properly.