1

Ive created a nested model form the RailsCasts Nested Model Form Part 2 example and have it working with Rails 3 and JQuery. Currently, a user can create a new project and click a link to add new tasks, and within each new task, click a link that will allow them to add new assignments to each task. Here's the link to create a new task (the link to create new assignments is similar):

        <p><%= link_to_add_fields "Add task", f, :task %></p>

That refers to this helper:

  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 => "new_#{association}") do |builder|
  render(association.to_s.singularize + "_fields", :f => builder)
end
link_to_function(name, h("add_fields(this, \"#{association}\", \"#{escape_javascript(fields)}\")"))end

It works like this: By clicking the link, html is added through JQuery that displays a form field for the task's name and a link that allows a user to add new assignments to the task.

What I'd like to do is have the assignment forms appear alongside the task name field - so that the user does not need to click an additional link to add a new assignments to each task. I can accomplish this when the page first loads through the project controller:

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

However, because Im using JQuery to add and remove HTML, the controller won't be called and the forms won't be build when 'adding' new tasks.

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

Here's my partial for the task form that is created when a user clicks the 'new task' link in the form:

<div class = "fields">
<%= f.label :name, "Task name"%>
<%= link_to_remove_fields "remove", f %><br />
<%= f.text_field :name %>



<% f.fields_for :assignments do |builder| %>
    <%= render 'assignment_fields', :f => builder %>
<% end %>

<p><%= link_to_add_fields "Add assignments", f, :assignments %></p>

2
  • Not sure if you're still looking for a answer, but there's been a revised railscast on this railscasts.com/episodes/196-nested-model-form-revised Commented Mar 26, 2013 at 16:56
  • I'd also recommend using the cocoon gem to handle the nested form parts. Commented Mar 26, 2013 at 16:57

2 Answers 2

1

I have a similar situation and I am currently (like you) looking for a solution. From Rails 3 source I found:

activerecord/lib/active_record/nested_attributes.rb:

# === One-to-many
#
# Consider a member that has a number of posts:
#
#   class Member < ActiveRecord::Base
#     has_many :posts
#     accepts_nested_attributes_for :posts
#   end
#
# You can now set or update attributes on an associated post model through
# the attribute hash.
#
# For each hash that does _not_ have an <tt>id</tt> key a new record will
# be instantiated, unless the hash also contains a <tt>_destroy</tt> key
# that evaluates to +true+.

I hope this gives you ideas.

Sign up to request clarification or add additional context in comments.

Comments

0

You can simulate a click of the add assignments link with jQuery once the task form is created. That way you don't depend on a rails controller at all.

after_create_handler = function() { $('assignment add link identifier').click() }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.