title: Rails nested resources and routing - how to break up controllers?
body: <p>I have the following models:</p>
  
  <ul>
  <li>Post</li>
  <li>Tag</li>
  <li>TaggedPost (from which Post and Tag derive their associations by has_many :through)</li>
  </ul>
  
  <p>And I have the following <code>routes.rb</code> file:</p>
  
  <pre><code>resources :tags
  
  resources :posts do
    resources :tags
  end
  </code></pre>
  
  <p>So when I navigate to, say, <code>/posts/4/tags</code>, that will shoot me into the index action for the Tag controller with the <code>post_id</code> value set in the parameters array.  Cool.</p>
  
  <p>My question is though, now that I'm accessing the nested tags resource under posts, should I be hitting the Tags controller still?  Or should I setup some other controller to handle the nested nature of tags at this point?  Otherwise I have to build additional logic into the Tags controller.  This can be done of course, but is this the common way of handling nested routes and resources?  The code I have in the index action for the Tags controller is as follows:</p>
  
  <p><strong>TagsController.rb</strong></p>
  
  <pre><code>def index
    if params[:post_id] &amp;&amp; @post = Post.find_by_id(params[:post_id])
      @tags = Post.find_by_id(params[:post_id]).tags
    else
      @tags = Tag.order(:name)
    end
    respond_to do |format|
      format.html
      format.json {render json: @tags.tokens(params[:q]) }
    end
  end
  </code></pre>
  
  <p>I can see the code in this controller growing increasingly large, as I plan for many additional resources to be associated with tag resources.  Thoughts on how to break this out?</p>
  
  <p>Summary of questions:</p>
  
  <ol>
  <li>If a resource is nested, should the nested resource be going through a different controller representing the nested nature of the resource?  This is opposed to going through the normal controller as I am in the code example that I provided.</li>
  <li>If so, how should these controllers be named and setup?</li>
  </ol>
  
  <p>Let me know if you need more information.</p>