title: Rails Namespace vs. Nested Resource
body: <p>Let's say my app has two models, Foo and Bar.</p>
  
  <p>Foo optionally belongs_to Bar.</p>
  
  <p>Right now I can look at a single Foo, or search for a particular Foo, and the FoosController handles all that. My URLS are like:
  <code>foos/1</code> and <code>foos/new</code></p>
  
  <p>Sometimes I want to look at a Bar. The BarsController handles that, and I get to it like:
  <code>bars/1</code> or <code>bars/1/edit</code>.</p>
  
  <p>If I'm looking at a Bar I might want to browse all the Foos that are part of that Bar. So, I'd like to use <code>bars/1/foos/</code> to look at those Foos.</p>
  
  <p>This is pretty straightforward with nested resources, and it looks like this:</p>
  
  <pre><code>resources :foo
  resources :bar do
    resources :foo
  end
  </code></pre>
  
  <p>However, Foos that are part of a Bar are kind of special, set apart from regular Foos. So, for instance, if I load <code>foos/1</code> or <code>bars/1/foos/1</code>, I would be looking at the same Foo, but I am focused on different information in each case.</p>
  
  <p>So I've been thinking about having a BarFoos Controller to handle Foos when they're in the context of a Bar. However, if I nest BarFoos under Bar, then my helpers are going to be like <code>bar_bar_foos_path</code> and <code>new_bar_bar_foo_path</code>. That seems redundant.</p>
  
  <p>So, now I'm thinking about namespaces, which is something I've never looked into before. I see in the rails guide that I could define:</p>
  
  <pre><code>namespace "bar" do
    resources :foos
  end
  </code></pre>
  
  <p>If I do that I can make a second <code>FoosController</code> under <code>app/bar/</code>, and that FoosController can handle Foos inside of a Bar with nice helpers like <code>bar_foo_path(:id)</code> instead of <code>bar_bar_foo_path(:id)</code>.</p>
  
  <p>But if I do that, what happens to my <code>BarsController</code>? How do requests get routed to <code>BarsController</code> if instead of <code>resources :bars</code> I have <code>namespace "bar"</code>?</p>
  
  <p>And, lastly, is there anything special I need to do inside my secondary FoosController to make sure there's not a name conflict with the top-level FoosController? I realize the routing says "namespace", but how does the rest of the ruby code know that the <code>app/bar/foos_controller</code> and <code>app/foos_controller</code> are not the same class?</p>
  
  <p>Thanks!</p>