【发布时间】:2014-07-18 15:33:05
【问题描述】:
我正在尝试创建基于选定父对象的动态下拉列表。
我的模型是社区、地段、住宅和社区住宅
class Community < ActiveRecord::Base
has_many :lots
has_many :community_homes
has_many :communities, through: :community_homes
end
------------------------------------------------------------------
class CommunityHome < ActiveRecord::Base
belongs_to :community
belongs_to :home
end
------------------------------------------------------------------
class Lot < ActiveRecord::Base
belongs_to :community
end
------------------------------------------------------------------
class Home < ActiveRecord::Base
has_many :community_homes
has_many :communities, through: :community_homes
end
-------------------------------------------------------------------
表单有一个包含所有社区的下拉列表。我需要其他下拉列表来仅显示与所选社区相关联的地段和房屋。
例如,我的一个社区有 29 个地块和 14 个平面图(房屋)。选择该社区时,我希望这29个批次和14所房屋成为下拉列表中唯一的对象,而不是所有198个批次和45个家庭。任何帮助和/或正确方向的简单点将不胜感激......谢谢!
我选择社区、地段和住宅的形式是合同。 Homes CRUD 独立于社区和合同,然后通过关联通过 has_many 加入社区。地段被创建为属于社区。
class Contract < ActiveRecord::Base
validates :customer_name, :customer_phone_number, presence: true
has_one :community
has_one :lot
has_one :home
end
合同表格
= simple_form_for @contract do |f|
.community-select
= f.collection_select( :community, Community.all, :id, :name, {}, { :multiple => false } )
%br
.lot-select
= f.collection_select( :lot_number, Lot.all, :id, :number, {}, { :multiple => false } )
%br
.inputs
= f.text_field :property_address, placeholder: "Property Address", class: "myform-control"
%br
= f.text_field :customer_name, placeholder: "Customer Name", class: "myform-control"
%br
= f.text_field :customer_phone_number, placeholder: "Customer Phone Number", class: "myform-control"
%br
= f.text_field :selection_date, placeholder: "Selection Date", class: "myform-control"
%br
.home-select
= f.collection_select(:home, Home.all, :name, :name, {}, {:multiple => false } )
%br
= f.submit 'Save', class: "btn btn-success"
= link_to "Cancel", contracts_path, type: "button", class: "btn btn-danger"
我正在为 .json 使用 gem active_model_serializer
【问题讨论】:
标签: javascript jquery ruby-on-rails ajax json