【发布时间】:2013-02-25 17:35:40
【问题描述】:
感谢您的宝贵时间。
我正在使用 rails 3.2,我正在使用 gems simple_form、cocoon 和 rails3-jquery-autocomplete。
我有以下型号Machine、Part、PartUsage、MachineCosting 和 PartCosting
具有多对多关联的机器和零件模型。
class Machine < ActiveRecord::Base
attr_accessible :name
has_many :part_usages
has_many :parts, :through => :part_usage
end
class Part < ActiveRecord::Base
attr_accessible :name
end
class PartUsage < ActiveRecord::Base
attr_accessible :machine_id,
:part_id
belongs_to :part
belongs_to :machine
end
具有一对多关联的机器成本和零件成本模型。
class MachineCosting < ActiveRecord::Base
attr_accessible :machine_id,
:total_cost,
:machine_name,
:part_costings_attributes
attr_accessor :machine_name
has_many :part_costings, :dependent => :destroy
accepts_nested_attributes_for :part_costings, :allow_destroy => true
def machine_name
self.machine.try(:name)
end
end
class PartCosting < ActiveRecord::Base
attr_accessible :part_id,
:part_name,
:cost
attr_accessor :part_name
belongs_to :machine_costing
belongs_to :part
def part_name
self.part.try(:name)
end
end
MachineCosting 表格
意见/machine_costings/_form.html.erb <%= simple_form_for(@machine_costing, :html => {:multipart => true}) do |f| %>
<%= f.input :machine_id, :url => autocomplete_machine_id_machines_path,
:as => :autocomplete %>
<!-- HERE, WHENEVER I SELECT A MACHINE, I WANT TO ADD NESTED-FORMS FOR
PARTCOSTINGS CORRESPONDING TO ALL THE PARTS OF THE MACHINE I JUST SELECTED.-->
<%= f.simple_fields_for :part_costings do |part_costing|%>
<%= render 'part_costings/part_costing_fields', :f => part_costing %>
<% end %>
<% end %>
请建议我如何在字段中选择 machine_id 后使用 javascript 为 PartCostings 填充和动态添加固定字段数。
我很乐意提供更多信息。
再次感谢!!
【问题讨论】:
标签: javascript jquery ruby-on-rails-3 simple-form cocoon-gem