Note that there are some explanatory texts on larger screens.

plurals
  1. POOrganizing the Controller Directory in Rails (3)
    text
    copied!<p>I'm developing an application that is primarily an API gateway. In the expectation that we'll be developing multiple versions of the API over time and with the interest of having backward compatibility, I am looking to have something along the lines of:</p> <p><a href="http://host.domain.com/apiv1/:token/:module(/:id(:method" rel="nofollow">http://host.domain.com/apiv1/:token/:module(/:id(:method</a>))</p> <p>Given this, what I am looking to do is to have a sub-routing system of my own within each API. What I'd like to have in terms of a file structure in the Controller directory is something akin to the following:</p> <pre><code>/app/controllers/apiv1_controller.rb /app/controllers/apiv1/module_controller.rb /app/controllers/apiv1/response_controller.rb </code></pre> <p>and eventually also have:</p> <pre><code>/app/controllers/apiv2_controller.rb /app/controllers/apiv2/module_controller.rb /app/controllers/apiv2/response_controller.rb </code></pre> <p>What this breaks down to is that I am unsure how to call methods within the controllers in the subdirectories, using something like:</p> <pre><code>return Apiv1::ResponseController.index </code></pre> <p>gives me:</p> <pre><code>undefined method `index' for Apiv1::ResponseController:Class </code></pre> <p>Any leads? Does this setup require that I explicitly "require" the requisite file manually?</p> <p>Pasted Here in response to the question:</p> <p><strong>routes.rb</strong></p> <pre><code>AppName::Application.routes.draw do resources :users match 'api-v1/:token/:module(/:id(/:method))' =&gt; 'apiv1#route' root :to =&gt; "welcome#index" end </code></pre> <p><strong>apiv1_controller.rb</strong></p> <pre><code>class Apiv1Controller &lt; 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 </code></pre> <p><strong>apiv1/response_controller.rb</strong></p> <pre><code>class Apiv1::ResponseController &lt; ApplicationController protect_from_forgery respond_to :json def index(params) Rails.logger.level = 0 logger.info("INDEX ACTION") result = { 'success' =&gt; true, 'controller' =&gt; 'response', 'api' =&gt; 'v1' } render :json =&gt; result end end </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload