ruby, rails, railscasts example I messed up

Posted by Sam on Stack Overflow See other posts from Stack Overflow or by Sam
Published on 2010-04-10T05:23:49Z Indexed on 2010/04/10 6:33 UTC
Read the original article Hit count: 361

Filed under:

If you saw the railscasts on nested forms this is the helper method to create links dynamically. However, after I upgraded to ruby 1.9.2 and rails 3 this doesn't work and I have now idea why.

    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

here is the javascript

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

}

When I view source this is how the link is getting rendered:

<p><a href="#" onclick="add_fields(this, &quot;dimensions&quot;, &quot;&quot;); return false;">Add Dimension</a></p>

so &quot;&quot; is not the correct information to build a new template and something is going on with how the string for fields is getting set. such as fields= f.fields_for

© Stack Overflow or respective owner

Related posts about ruby-on-rails