ruby - Organizing the Controller Directory in Rails (3) -
i'm developing application api gateway. in expectation we'll developing multiple versions of api on time , interest of having backward compatibility, looking have along lines of:
http://host.domain.com/apiv1/:token/:module(/:id(:method))
given this, looking have sub-routing system of own within each api. i'd have in terms of file structure in controller directory akin following:
/app/controllers/apiv1_controller.rb /app/controllers/apiv1/module_controller.rb /app/controllers/apiv1/response_controller.rb
and have:
/app/controllers/apiv2_controller.rb /app/controllers/apiv2/module_controller.rb /app/controllers/apiv2/response_controller.rb
what breaks down unsure how call methods within controllers in subdirectories, using like:
return apiv1::responsecontroller.index
gives me:
undefined method `index' apiv1::responsecontroller:class
any leads? setup require explicitly "require" requisite file manually?
pasted here in response question:
routes.rb
appname::application.routes.draw resources :users match 'api-v1/:token/:module(/:id(/:method))' => 'apiv1#route' root :to => "welcome#index" end
apiv1_controller.rb
class apiv1controller < applicationcontroller protect_from_forgery respond_to :json def route rails.logger.level = 0 logger.info("route action") logger.info("params: #{params}") apiv1::responsecontroller.index(params) end end
apiv1/response_controller.rb
class apiv1::responsecontroller < applicationcontroller protect_from_forgery respond_to :json def index(params) rails.logger.level = 0 logger.info("index action") result = { 'success' => true, 'controller' => 'response', 'api' => 'v1' } render :json => result end end
if looking versioning rest apis, can use restful_route_version gem. takes care of inheriting 1 version don't rewrite same rest apis every version.
Comments
Post a Comment