Ruby On Rails : Routing Concerns

  • One of the fundamental principles Rails developers follow is Donโ€™t Repeat Yourself (DRY).๐Ÿ”
  • We can use concerns, to avoid repeating the same routes defined.
  • To eliminate code duplication rails introduced the routing method :concern.
concern :commentable do
   resources :comments

concern :imageable do
  resources :images

resources :users, concerns: :imageable
resources :posts, concerns: %i[commentable imageable]

The :concerns option can accept one or more routing concerns.