jquery - Automatically build a nested model form with using the 'link_to_add_fields helper' -


Ive created nested model form RailsCasts nested model form part 2 example and it is working with Rail 3 and JQuery At present, the user can create a new project and click on a link to add new tasks, and within each new task, click on the link that will allow the new assignment to be added for each task. . Here's a link to create a new task (the link to create a new assignment is the same):

  & lt; P & gt; & Lt;% = link_to_add_fields "Add Actions", f,: task%> & Lt; / P & gt;  

This refers to this assistant:

  def link_to_add_fields (name, f, association) new_object = f.object.class.reflect_on_association (association) .klass .new fields = f.fields_for (association, new_object ,: child_index = & gt; "new_ # {association}") | Manufacturer | Render (association.ToSecilarize + "_feeds", f = & gt; Builder) end link_to_function (name, h ("add_fields (this, \" # {union} \ ", \" # {escape_javascript (fields)} \ " ) End:  

It works like this: By clicking the link, the HTML has been added through JQuery which displays a form field and a link for the name of the work Does the user allow to add new tasks for the job

What I want to do, assign with the task name field Appears in the form - so that the user does not need to click on additional links to add a new task for each task, when I first load the page through the project controller, I can complete it:

  1.times do task = @ product.tasks.build 1.times {task.assignments.build} end  

However, because I add HTML and Using JQuery to delete, the controller will not be called and new functions Forms will not be created when adding them.

Is there a way to 'build' the nested assignment field automatically (from work partial) when the new work area is created?

Here's my partial work form that was created when a user clicks the 'New Job' link in this form:

   & Lt;% = link_to_remove_fields "Remove", f%> & Lt; Br / & gt; & Lt;% = f.text_field: name% & gt; & Lt;% f.fields_for: Assign | Builder | & Gt%; & Lt;% = Render 'assignment_flords' ,: f = & gt; Builder% & gt; & Lt;% end% & gt; & Lt; P & gt; & Lt;% = link_to_add_fields "Add assignment", F,: Assignment%> & Lt; / P & gt;  

I have a similar status and I currently (Like you) looking for a solution from Rail 3 source I found:

activerecord / lib / active_record / nested_attributes.rb:

  # === One-to-one -To # # Consider a member who has many posts: # # Class Members & lt; ActiveRecord :: Base # has_many: posts # accepts_nested_attributes_for: posts # end # # You can now set or update attributes on the related post model through the attribute hash. # # For each hash that _not_ does a & lt; Tt & gt; Id & lt; / Tt & gt; The key will be a new # record, as long as a & lt; Tt & gt; _destroy & lt; / Tt & gt; The key # that evaluates + true +  

I hope this will give you ideas.


Comments