本文介绍了如何在Ruby on Rails中创建嵌套表单?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下安排

Pizza模型(用于创建客户可以订购的比萨饼列表)也与订单相关联,以指示已订购的比萨饼.

The Pizza model, to create a list of pizzas that can be ordered by customers, also gets associated with order, so to indicate which pizza has been ordered.

class Pizza < ActiveRecord::Base
  has_many :pizza_orders
  has_many :orders, :through => :pizza_orders
  has_and_belongs_to_many :toppings
end

选项模型,用于创建可以与某些比萨饼相关联的选项的列表,并且还与每个比萨饼订单的联接表相关联,以指定哪个比萨饼具有排头顺序.

Option model, to create a list of options that can be associated with certain pizzas, also gets associated with the join table for each pizza order, to specify which pizza has the topping ordered.

class Topping < ActiveRecord::Base
  has_and_belongs_to_many :pizzas
  has_and_belongs_to_many :pizza_orders
end

这是比萨和浇头的联接表,这是必需的,因为没有它,您将无法指定可以或不能与比萨一起订购的浇头.毕竟,将意大利辣香肠馅料列在素食比萨上可能会冒犯某人.

The join table for pizza and topping, this is needed because without it, you can not specify which toppings can or can not be ordered with a pizza. After all, having peperoni topping listed to a vegetarian pizza might offend someone.

class PizzasToppings < ActiveRecord::Base
  belongs_to :pizza
  belongs_to :topping
end

订单模型,它仅将所有联接表保持在一起.

The order model, this just holds all the join tables together.

class Order < ActiveRecord::Base
  has_many :pizza_orders
  has_many :pizzas, :through => :pizza_orders
end

披萨和订单之间的联接表,这个多对多是具有许多贯通性,而不是具有并且属于许多,因为在Rails中,您不能直接操纵HBATM联接表(据我所试),并且您需要具有选择权关系.

The join table between pizza and order, this many to many is a has many through, and not a has and belongs to many since in Rails you cannot directly manipulate HBATM join tables (as far as I have tried), and you need to be able to because of the options relation ship.

class PizzaOrder < ActiveRecord::Base
  belongs_to :pizza
  belongs_to :order

  has_and_belongs_to_many :toppings
end

联接表,用于指示已为订单中的特定比萨选择了哪些浇头.

The join table to indicate which toppings have been chosen for a particular pizza in a order.

class PizzaOrdersToppings < ActiveRecord::Base
  belongs_to :pizza_orders
  belongs_to :topping
end

然后,我有一个管理页面来创建和关联比萨饼和浇头.

Then I have an admin page to create and associate the Pizzas and toppings.

但是我不知道如何创建订单表格.用户应该能够添加披萨并选择一个或多个已经创建的浇头

But I don't know how to create the orders form. The user should be able to add a pizza and select one or many toppings which are already created

推荐答案

截屏有点旧了嵌套模型第1部分(由ryan bates制作),但我希望它对您有所帮助,而且此截屏视频也有修订版.

Its a bit old screencast Nested model form part 1 made by ryan bates but i hope it helps you , also there is a revised version of this screencast.

这篇关于如何在Ruby on Rails中创建嵌套表单?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!