Rails 嵌套路由表单问题
Rails nested route form issue
我有一个包含很多产品的类别模型。以下是路线:
resources :categories do
resources :products
end
我在 category_product_new 的 rake 路由中看到一条路由。当我尝试创建新产品时,我收到错误消息,指出我尝试过的任何东西都没有路径。
<%= form_with(model: product, local: true) do |form| %>
关于我需要更改表单以允许我提交它的任何想法?
你可以试试:
# app/views/products/new.html.erb
<%= form_with(model: [@category, @product], local: true) do |form| %>
// your code here
<% end %>
在你的 products_controller
中,你可以这样尝试:
# app/controllers/products_controller.rb
class ProductsController < ApplicationController
def new
@category = Category.find(params[:category_id])
@product = Product.new
end
end
谢谢:)
我有一个包含很多产品的类别模型。以下是路线:
resources :categories do
resources :products
end
我在 category_product_new 的 rake 路由中看到一条路由。当我尝试创建新产品时,我收到错误消息,指出我尝试过的任何东西都没有路径。
<%= form_with(model: product, local: true) do |form| %>
关于我需要更改表单以允许我提交它的任何想法?
你可以试试:
# app/views/products/new.html.erb
<%= form_with(model: [@category, @product], local: true) do |form| %>
// your code here
<% end %>
在你的 products_controller
中,你可以这样尝试:
# app/controllers/products_controller.rb
class ProductsController < ApplicationController
def new
@category = Category.find(params[:category_id])
@product = Product.new
end
end
谢谢:)