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":plex 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!
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":plex 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!
Share Improve this question asked May 21, 2014 at 8:22 oliveroliver 1332 silver badges12 bronze badges3 Answers
Reset to default 4link_to_function is deprecated in rails 4.1 and it is remended 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
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
I am assuming you made this helper from the rails cast episode http://railscasts./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" %>