[英]Form with two different nested models which exclude each other
I have a form for one model with a couple of input boxes and one dropdown box where type ( TypeA
and TypeB
) of the model can be picked. 我有一个带有两个输入框和一个下拉框的模型模型,可以在其中选择模型的类型(
TypeA
和TypeB
)。 Let's say models are something like this: 假设模型是这样的:
// car has columns name and country
class Car < ActiveRecord::Base
end
// has columns a_property_1 and a_property2
class TypeAProperties < ActiveRecord::Base
belongs_to :cars
end
// has columns b_property_1 and b_property2
class TypeBProperties < ActiveRecord::Base
belongs_to :cars
end
After user opens form, by default there should be displayed fields for name, country, dropdown box with TypeA
selected, a_property_1 and a_property2. 用户打开表单后,默认情况下将显示名称,国家/地区,选择
TypeA
下拉框,a_property_1和a_property2的字段。
If user navigates to dropdown box and selects TypeB
, a_property_1 and a_property2 should disappear from screen, and b_property_1 and b_property2 should appear on screen. 如果用户导航到下拉框并选择
TypeB
,则a_property_1和a_property2应该从屏幕上消失,而b_property_1和b_property2应该出现在屏幕上。 So, TypeAProperties and TypeBProperties are created depending on selected value from drop down box. 因此,根据从下拉框中选择的值来创建TypeAProperties和TypeBProperties。 Car can have only one property, A or B.
汽车只能具有A或B的一种属性。
Any ideas how to handle this situation? 有什么想法如何处理这种情况? I am a little bit stuck with this.
我对此有些坚持。 Thank you.
谢谢。
I believe you should do vise versa, so: 我相信您也应该这样做,所以:
class Cars < ActiveRecord::Base
belongs_to :car_type, polymorphic: true
end
and in type classes 和类型类
class TypeAPropertySet < ActiveRecord::Base
has_one :car, as: :car_type # or has_many
end
class TypeBPropertySet < ActiveRecord::Base
has_one :car, as: :car_type # or has_many
end
Please refer to the document on the rails polymorphism. 请参考关于Rails多态性的文档 。 So then you be able to use
Car
s properties, along with the car_type
in the form. 这样便可以使用
Car
的属性以及表单中的car_type
了。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.