【问题标题】:rails 4 nested resource routing errorrails 4嵌套资源路由错误
【发布时间】:2014-04-15 16:26:34
【问题描述】:

我有一个路线文件(Rails 4.0.4 应用程序):

resources :products do
  resources :variants, except: :index
end

rake routes 给出:

       product_variants POST   /products/:product_id/variants(.:format)             variants#create
    new_product_variant GET    /products/:product_id/variants/new(.:format)         variants#new
   edit_product_variant GET    /products/:product_id/variants/:id/edit(.:format)    variants#edit
        product_variant GET    /products/:product_id/variants/:id(.:format)         variants#show
                        PATCH  /products/:product_id/variants/:id(.:format)         variants#update
                        PUT    /products/:product_id/variants/:id(.:format)         variants#update
                        DELETE /products/:product_id/variants/:id(.:format)         variants#destroy

所以按照建议我正在使用

<%= form_for [@product, @variant] do |f| %>

但这会生成(注意 URL):

<form accept-charset="UTF-8" action="/variants/1" class="edit_variant" id="edit_variant_1" method="post">

我(当然)得到路由错误,说没有路由与 PATCH /variants/1 匹配

当我重定向到位于 api/ 下的命名空间的 Api 控制器中时出现同样奇怪的行为:

redirect_to [:api, @product, @variant]

知道了:没有与 /api/variants/1 匹配的路由

所以:

  • 我检查了所有路线
  • 我知道如何嵌套资源和命名它们
  • 我以为我知道如何生成 URL 的 ;-)

生成网址时,@products 会以某种方式被忽略

有没有人知道在哪里可以进一步查看?

【问题讨论】:

  • 可以确认@product变量不为nil。

标签: ruby-on-rails ruby ruby-on-rails-4 routing


【解决方案1】:

查看您的变体控制器。你在实例化@product 吗?

发布控制器的代码也可能会有所帮助。

希望这会有所帮助!

亚历克斯

【讨论】:

    【解决方案2】:

    好的,问题确实出在不同的控制器上。我以某种方式在之前的操作中实例化了@products,但显然做错了。

    似乎是能量饮料问题。感谢您为我指出正确的解决方案!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-04-07
      • 2016-03-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多