Note that there are some explanatory texts on larger screens.

plurals
  1. PODynamically adding prepopulated nested forms using cocoon and rails3-jquery-autocomplete
    text
    copied!<p>Thanks for your time.</p> <p>I am working on rails 3.2 and I am using gems <a href="https://github.com/plataformatec/simple_form" rel="nofollow">simple_form</a>, <a href="https://github.com/nathanvda/cocoon" rel="nofollow">cocoon</a>, and <a href="https://github.com/crowdint/rails3-jquery-autocomplete" rel="nofollow">rails3-jquery-autocomplete</a>.</p> <p>I have following models <strong>Machine, Part, PartUsage, MachineCosting and PartCosting</strong></p> <p>Machine and Part models with many-to-many association.</p> <pre><code> class Machine &lt; ActiveRecord::Base attr_accessible :name has_many :part_usages has_many :parts, :through =&gt; :part_usage end class Part &lt; ActiveRecord::Base attr_accessible :name end class PartUsage &lt; ActiveRecord::Base attr_accessible :machine_id, :part_id belongs_to :part belongs_to :machine end </code></pre> <p>MachineCosting and PartCosting models with one-to-many association.</p> <pre><code> class MachineCosting &lt; ActiveRecord::Base attr_accessible :machine_id, :total_cost, :machine_name, :part_costings_attributes attr_accessor :machine_name has_many :part_costings, :dependent =&gt; :destroy accepts_nested_attributes_for :part_costings, :allow_destroy =&gt; true def machine_name self.machine.try(:name) end end class PartCosting &lt; 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 </code></pre> <p>Form for MachineCosting</p> views/machine_costings/_form.html.erb <pre><code> &lt;%= simple_form_for(@machine_costing, :html =&gt; {:multipart =&gt; true}) do |f| %&gt; &lt;%= f.input :machine_id, :url =&gt; autocomplete_machine_id_machines_path, :as =&gt; :autocomplete %&gt; &lt;!-- 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.--&gt; &lt;%= f.simple_fields_for :part_costings do |part_costing|%&gt; &lt;%= render 'part_costings/part_costing_fields', :f =&gt; part_costing %&gt; &lt;% end %&gt; &lt;% end %&gt; </code></pre> <p>Please suggest how can I populate and dynamically add fixed no of fields for PartCostings using javascript after machine_id is selected in the field.</p> <p>I would be happy to provide any more information. </p> <p>Thanks again!! </p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload