1
  def link_to_add_nested_fields(name, f, association, klasss, type) 
    new_object = f.object.send(association).klass.new
    id = new_object.object_id
    field = f.fields_for(association, new_object, child_index: id) do |builder|
      render(association.to_s.singularize + "_#{type}", f: builder)
    end
    link_to(name, '#', class: klasss, data: {id: id, type: field.gsub("\n", "")})   
  end

I'm trying to customise a piece a helper code i got from here http://railscasts.com/episodes/196-nested-model-form-revised, but i'm having problems with the type parameter. An example of this helper being used;

<%= link_to_add_nested_fields "Add custom field", f, :fields, "add_fields","fields" %>

The problem is definitely with the type parameter, does anyone know how i can solve this? Thanks

1 Answer 1

1

I have seen this cast, and this doesn't seem to be the best solution for handling "Add More" link for nested attributes.

Basically, the way accepts_nested_attributes works is the key here.

In your controller :

parent.child.build

Just build it once for initial view. This will enable the field to appear initially on page reload.

In your .erb template,

<% parent.children.each do |child| %>
<div class="child_fields">
    <%= render "the child fields partial" %>
</div>
<% end %>
<%= link_to "Add More", "#", class: "add_more_link" %>
<% javascript_include_tag "js_file_to_handle_add_more_link" %>

In your "js_file_to_handle_add_more_link.js"

Firstly, count the existing child fields using:

$('.child_fields').size();

Now, create the html fields with id as:

parent_children_attributes_" + count + "_attribute_name"

And name as:

"parent[children_attributes]["+ count +"][attribute_name]"

Each set of child fields should have a unique value count. Also, here parent is singular while children is plural.

And, that is it.

Now When the form is submitted, rails will automatically save the child objects as each of it is identified by the accepts_nested_atributes format uniquely

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

4 Comments

Thanks for this, i'll try it out in my next project but right now i've a lot of code(both ruby and javascript) invested in this method to make it work for my needs :( Do you know how i can convert the type param from a string to just plain text? or better yet an hash key?
Can u explain this a little bit more?
I'm passing the string "fields" with the type parameter, so on the second to last line(the link_to), data is now; data: {id: id, "fields": field.gsub("\n", "")} because type is a string. What i want is data: {id: id, fields: field.gsub("\n", "")}. So fields not "fields", do you get?
{id: id, type: field.gsub("\n", "")}.symbolize_keys! . This should work here.

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.