3

i know there are some threads about the railscast nested forms... and that link_to_add_fields is not working proper in rails 4 anymore, because of the change about the unobstrusive js. can someone help me to understand and tell me what i do have to change in order to get my nested form working? i´m trying to "rebuild":complex form but he uses rails 3 and i´m rails 4. i do get the following error:

undefined method `link_to_function' for

in _form.html.erb:

<%= link_to_add_fields("Add a Contact", f, :contacts, :class => "btn btn-primary", :title => "Add a new Contact") %>

my application_helper.rb

def link_to_add_fields(name, f, association, options = {})
new_object = f.object.class.reflect_on_association(association).klass.new
fields = f.fields_for(association, new_object, :child_index => "new_#{ association }", :onsubmit => "return $(this.)validate();") do |builder|
  render(association.to_s.singularize + "_fields", :f => builder)
end

link_to_function(name, "add_fields(this, \"#{ association }\", \"#{ escape_javascript(fields) }\")", options)

end end

thanks!

3 Answers 3

4

link_to_function is deprecated in rails 4.1 and it is recommended the use of Unobtrusive JavaScript instead.

this is what I'd suggest:

use the jQuery code provided:

function add_fields(link, association, content) {  
    var new_id = new Date().getTime();  
    var regexp = new RegExp("new_" + association, "g");  
    $(link).parent().before(content.replace(regexp, new_id));  
}

in your form:

<%= link_to_add_fields "Add a Contact", f, :contacts, "btn btn-primary", "Add a new Contact" %>

then in application_helper:

def link_to_add_fields(name, f, association, cssClass, title)  
  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 name, "#", :onclick => h("add_fields(this, \"#{association}\", \"#{escape_javascript(fields)}\")"), :class => cssClass, :title => title 
end  
Sign up to request clarification or add additional context in comments.

Comments

4

I tweeked Mavis's code - specifically the link_to function (forgot to add remote: true)

Application.js

function add_fields(link, association, content) {  
    var new_id = new Date().getTime();  
    var regexp = new RegExp("new_" + association, "g");  
    $(link).parent().before(content.replace(regexp, new_id));  
}

Form code:

<%= link_to_add_fields "Add a Contact", f, :contacts, "btn btn-primary", "Add a new Contact" %>

This is the only change to Mavis's code:

application_helper.rb

    def link_to_add_fields(name, f, association, cssClass, title)  
  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 name, "#", :onclick => h("add_fields(this, \"#{association}\", \"#{escape_javascript(fields)}\")"), class: cssClass, title: title, remote: true 
end  

Comments

1

I am assuming you made this helper from the rails cast episode http://railscasts.com/episodes/197-nested-model-form-part-2.

In your application_helper.rb, you have defined link_to_add_fields. Probably something like this.

def link_to_add_fields(name, f, association)

change it to this

def link_to_add_fields(name, f, association, locals={})

Then on the return statement.

link_to_function(name, "add_fields(this, \"#{association}\", \"#{escape_javascript(fields)}\")", class: locals[:class])

Finally, use your new method like so

<%= link_to_add_fields "Add A Present", f, :presents, class: "btn btn-mini btn-info" %>

1 Comment

thanks. i tried this but the error ( undefined method...) is still showing up... if i take the whole line out, the form is showing like it´s suppose to be. in rails 3.2.9 it is working with no problems at all...

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.