I’m trying to pass a variable to a javascript function from a helper
function but I’m not getting it to work. I think the problem is with
#{domElement}
any ideas?
thanks!
This is how the helper function is called from the view:
<%= link_to_add_fields t(‘form.treatment.add’),
form, :treatments, :table %>
Then this is the helper function:
def link_to_add_fields(name, f, association, domElement)
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)}”, #{domElement})”))
end
And this is the javascript function:
function add_fields(link, association, content, domElement) {
var new_id = new Date().getTime();
var regexp = new RegExp(“new_” + association, “g”);
var table = $(link).up(domElement);
table.insert({
before: content.replace(regexp, new_id) });
}