So there's the following code from this railscast for dynamically adding in a field for a nested model form:
This was in the application 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
And this was the javascript function:
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));
}
Now I'm curious as to how you would change all of this code so that you could rely purely on JQuery and no prototype or rjs.
The only Prototype related thing here is link_to_function
call. What it does is create onclick
handler over your name
text that calls your add_fields
function.
So to be pure jquery you should use $().click (function (){...})
method instead. However exact implementation will differ depending on whether you want to insert javascript in your html file or call it unobtrusively.