ruby - Dynamic namespaced controllers w/ fallback in Rails -
i have bizarre requirement new rails application. need build application in routes defined in multiple namespaces (let me explain). want have application in school subjects (math, english, etc) namespaces:
%w[math english].each |subject| namespace subject.to_sym resources :students end end
this great , works requires me create namespaced studentscontroller
each subject means if add new subject need create new controller.
what create base::studentscontroller
, if, let's math::studentscontroller
exists used , if doesn't exist, can dynamically create controller , inherit base::studentscontroller
.
is possible? if how go implementing this?
with routes defined way:
%w[math english].each |subject| scope "/#{subject}" begin "#{subject.camelcase}::studentscontroller".constantize resources :students, controller: "#{subject}::students", only: :index rescue resources :students, controller: "base::students", only: :index end end end
rake routes
outputs:
students /math/students(.:format) base::students#index /english/students(.:format) english::students#index
if english/students_controller.rb present , math/students_controller. absent.
Comments
Post a Comment