[英]rails 4 nested resource routing error
我有一个路由文件(Rails 4.0.4 App):
resources :products do
resources :variants, except: :index
end
犁耙路线可提供:
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 Controller时,同样的奇怪行为:
redirect_to [:api, @product, @variant]
告诉我: 没有与/ api / variants / 1匹配的路线
所以:
生成网址时以某种方式忽略@products
有谁知道在哪里进一步寻找线索?
看一下您的变体控制器。 您要实例化@product吗?
也为控制器发布代码可能会有所帮助。
希望这可以帮助!
亚历克斯
好的,问题确实出在不同的控制器上。 我以某种方式在事前操作中实例化了@products,但显然做错了。
似乎是能量饮料问题。 感谢您为我指出正确的解决方案!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.