title: Rails 3 routing with resources under an optional scope
body: <p>I have <a href="http://www.starkiller.net/2011/03/17/versioned-api-1/" rel="nofollow noreferrer">setup my versioned API</a> like this with only a small tweak for backwards compatibility. In my routes I have:</p>
  
  <pre><code>scope '(api(/:version))', :module =&gt; :api, :version =&gt; /v\d+?/ do
      …
      scope '(categories/:category_id)', :category_id =&gt; /\d+/ do
          …
          resources :sounds
          …
      end
  end
  </code></pre>
  
  <p>with the successful goal already reached of having the following URL's reach the same place</p>
  
  <pre><code>/api/v1/categories/1/sounds/2
  /api/categories/1/sounds/2
  /categories/1/sounds/2
  /sounds/2
  </code></pre>
  
  <p>My directory structure is like this:</p>
  
  <p><img src="https://i.stack.imgur.com/dw61y.png" alt="enter image description here"></p>
  
  <p>The problem I am seeing is in my link generations in my views. For example, on the sound show page I have a <code>button_to</code> to delete the sound</p>
  
  <pre><code>&lt;%= button_to 'Delete', @sound, :confirm =&gt; 'Are you sure?', :method =&gt; :delete %&gt;
  </code></pre>
  
  <p>Which generates the following url in the form <code>action</code>:</p>
  
  <pre><code>"/api/sounds/1371?version=1371"
  </code></pre>
  
  <p>And furthermore, the <code>delete</code> method is not working, instead it is being sent as a <code>POST</code></p>
  
  <p>The interesting parts of <code>rake routes</code> are:</p>
  
  <pre><code>                     sounds GET    (/api(/:version))(/categories/:category_id)/sounds(.:format)                               {:controller=&gt;"api/sounds", :version=&gt;/v\d+?/, :action=&gt;"index", :category_id=&gt;/\d+/}
                              POST   (/api(/:version))(/categories/:category_id)/sounds(.:format)                               {:controller=&gt;"api/sounds", :version=&gt;/v\d+?/, :action=&gt;"create", :category_id=&gt;/\d+/}
                    new_sound GET    (/api(/:version))(/categories/:category_id)/sounds/new(.:format)                           {:controller=&gt;"api/sounds", :version=&gt;/v\d+?/, :action=&gt;"new", :category_id=&gt;/\d+/}
                   edit_sound GET    (/api(/:version))(/categories/:category_id)/sounds/:id/edit(.:format)                      {:controller=&gt;"api/sounds", :version=&gt;/v\d+?/, :action=&gt;"edit", :category_id=&gt;/\d+/}
                        sound GET    (/api(/:version))(/categories/:category_id)/sounds/:id(.:format)                           {:controller=&gt;"api/sounds", :version=&gt;/v\d+?/, :action=&gt;"show", :category_id=&gt;/\d+/}
                              PUT    (/api(/:version))(/categories/:category_id)/sounds/:id(.:format)                           {:controller=&gt;"api/sounds", :version=&gt;/v\d+?/, :action=&gt;"update", :category_id=&gt;/\d+/}
                              DELETE (/api(/:version))(/categories/:category_id)/sounds/:id(.:format)                           {:controller=&gt;"api/sounds", :version=&gt;/v\d+?/, :action=&gt;"destroy", :category_id=&gt;/\d+/}
  </code></pre>
  
  <p>and the server logs show:</p>
  
  <pre><code>Started POST "/api/sounds/1371?version=1371" for 127.0.0.1 at Fri May 06 23:28:27 -0400 2011
    Processing by Api::SoundsController#show as HTML
    Parameters: {"authenticity_token"=&gt;"W+QlCKjONG5i/buIgLqsrm3IHi5gdQVzFGYGREpmWYs=", "id"=&gt;"1371", "version"=&gt;371}
  </code></pre>
  
  <p>I am using JQuery as my UJS and have the most recent version of rails.js for JQuery:</p>
  
  <pre><code>&lt;script src="https://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js" type="text/javascript"&gt;&lt;/script&gt; &lt;!-- must be &gt;= 1.4.4 for the rails 3 link js to work—&gt; 
  &lt;script src="/javascripts/jquery-ujs/src/rails.js?1304736923" type="text/javascript"&gt;&lt;/script&gt;
  </code></pre>
  
  <p><br />
  <br />
  <br />
  I know this is <code>tl;dr</code> but my question is: "<strong>What do I need to put in my button_to link_to and other view tags to make the url helpers generate the correct path for my setup?</strong>" I have tried just about every combination I can think of but can never get them to end up in the right place.</p>