Mlle
2010-May-05 10:00 UTC
Passing a variable to a javascript function from a helper function
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) }); } -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Frederick Cheung
2010-May-05 10:59 UTC
Re: Passing a variable to a javascript function from a helper function
On May 5, 11:00 am, Mlle <emsto...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> 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} >Well you should probably look at the generated javascript and all but it looks like you''re not quoting domElement (ie you have just title rather than the string literal ''title''). Passing a fragment of javascript through h seems highly dubious - link_to_function should be taking care of that (but do check!) Fred> 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) }); > > } > > -- > You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. > To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. > For more options, visit this group athttp://groups.google.com/group/rubyonrails-talk?hl=en.-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.