Javascript 使用两个相互排斥的不同嵌套模型形成

Javascript 使用两个相互排斥的不同嵌套模型形成,javascript,jquery,ruby-on-rails,ruby-on-rails-4,Javascript,Jquery,Ruby On Rails,Ruby On Rails 4,我有一个模型的表单,有两个输入框和一个下拉框,可以在其中选择模型的类型(TypeA和TypeB)。假设模型是这样的: // 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

我有一个模型的表单,有两个输入框和一个下拉框,可以在其中选择模型的类型(
TypeA
TypeB
)。假设模型是这样的:

// 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
//car有name和country列
类Car
用户打开表单后,默认情况下应显示姓名、国家/地区、选择了
TypeA
的下拉框、属性1和属性2的字段

如果用户导航到下拉框并选择
类型b
,则a_属性_1和a_属性2应从屏幕上消失,b_属性_1和b_属性2应出现在屏幕上。因此,将根据从下拉框中选择的值创建TypeAProperties和TypebProperty。汽车只能有一个属性,A或B


你知道如何处理这种情况吗?我有点受不了了。谢谢。

我认为你应该这样做,反之亦然,因此:

class Cars < ActiveRecord::Base
   belongs_to :car_type, polymorphic: true
end
classcars
在打字课上

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
类类型属性集

请参阅on-the-rails多态性。因此,您可以使用
Car
s属性,以及表单中的
Car\u type

您需要为这两个特定模型显示一个通用表单吗?@МаъСъъъ,我需要显示一个表单,其中包含
Car
的输入字段和(
typea属性
typeb属性
)。如果选择选项B,我不确定如何仅创建Car和TypebProperty,而忽略TypebProperty。您在哪里选择B选项?@МаъСъъъъ,我在表单中思考,所以我不确定是否应该在
Car
中创建一个新列来保存类型。表单将类似于:| Car input fields |->|下拉菜单r选择类型|->|类型属性或类型属性输入字段|->|提交按钮|我相信你应该这样做反之亦然:在
Cars中:属于类型,polimorphic
,在
中类型有一个:car