我正在将一个项目从一个代码库迁移到另一个代码库,以清理大量积累的垃圾,转移到新的表示层等。
其中一种关系是规范的User has_many Order
,我已将其表示为嵌套资源routes.rb
:
resources :users do
resources :orders
end
所以我有命名路线user_orders_path(@user)
。如果用户有订单,它工作得很好,但如果用户没有订单,尝试渲染路由会出现以下错误:
ActionController::RoutingError (No route matches {:controller=>"orders",
:user_id=>#<User id: 472, account_id: nil, first_name: "Denis", last_name:
"Haskin", ...etc... , last_sign_in_ip: "127.0.0.1">})
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
(我在 中提供了完整的堆栈跟踪rails console
;由于某种原因,我没有在服务器日志中获得此错误的完整堆栈跟踪。)
我有点不知所措来解释(和解决)这个问题,我希望这可能会给某人敲响警钟。我在一个新的空项目中对此进行了测试,当然它工作正常。我想接下来我会尝试尽可能多地剥离这些模型和控制器,然后开始添加部分,直到错误再次出现。因为我正在迁移现有代码,所以很难将其隔离为一小段示例代码。
(冒着可能混淆问题的风险,我已经开始在这个项目中使用 Ryan Bates 的cancan,我有点怀疑它可能在这里发挥作用,但我确实把它去掉并在没有它的情况下进行测试,没有改变这种不正确的行为。)