Note that there are some explanatory texts on larger screens.

plurals
  1. PO"ActionController::RoutingError: No route matches" error when no nested resource
    primarykey
    data
    text
    <p>I'm migrating a project from one codebase to another to clean up a lot of accumulated cruft, shift to a new presentation layer, etc.</p> <p>One of the relationships is the canonical <code>User has_many Order</code>, which I have expressed as nested resources in <code>routes.rb</code>:</p> <pre><code>resources :users do resources :orders end </code></pre> <p>and so I have the named route <code>user_orders_path(@user)</code>. It works just fine if the user has orders, but if the user has no orders, trying to render the route gives the following error:</p> <pre><code>ActionController::RoutingError (No route matches {:controller=&gt;"orders", :user_id=&gt;#&lt;User id: 472, account_id: nil, first_name: "Denis", last_name: "Haskin", ...etc... , last_sign_in_ip: "127.0.0.1"&gt;}) from /Users/dwhsix/.rvm/gems/ruby-1.9.3-p0@zmx2/gems/actionpack-3.2.3/lib/action_dispatch/routing/route_set.rb:532:in `raise_routing_error' from /Users/dwhsix/.rvm/gems/ruby-1.9.3-p0@zmx2/gems/actionpack-3.2.3/lib/action_dispatch/routing/route_set.rb:528:in `rescue in generate' from /Users/dwhsix/.rvm/gems/ruby-1.9.3-p0@zmx2/gems/actionpack-3.2.3/lib/action_dispatch/routing/route_set.rb:520:in `generate' from /Users/dwhsix/.rvm/gems/ruby-1.9.3-p0@zmx2/gems/actionpack-3.2.3/lib/action_dispatch/routing/route_set.rb:561:in `generate' from /Users/dwhsix/.rvm/gems/ruby-1.9.3-p0@zmx2/gems/actionpack-3.2.3/lib/action_dispatch/routing/route_set.rb:586:in `url_for' from /Users/dwhsix/.rvm/gems/ruby-1.9.3-p0@zmx2/gems/actionpack-3.2.3/lib/action_dispatch/routing/url_for.rb:148:in `url_for' from /Users/dwhsix/.rvm/gems/ruby-1.9.3-p0@zmx2/gems/actionpack-3.2.3/lib/action_dispatch/routing/route_set.rb:213:in `user_orders_path' from (irb):24 </code></pre> <p>(I'm providing the full stack trace from testing this in <code>rails console</code>; for some reason I'm not getting full stack trace for this error in the server log.)</p> <p>I'm sort of at a loss to explain (and fix) this, and I was hoping this might ring a bell with someone. I tested this in a new empty project and of course it works fine. I guess the next thing I'll try is to strip down as much as I can about these models and the controller, and start adding pieces back until the error recurs. Because I'm migrating over existing code, it's very difficult to isolate this to a small piece of example code.</p> <p>(At the risk of possibly confusing the issue, I have started using Ryan Bates' <a href="https://github.com/ryanb/cancan/" rel="nofollow">cancan</a> in this project and I'm a little suspicious that it might be playing a role here, but I did strip it out and test without it, with no change in this incorrect behavior.)</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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