3

I'm trying to implement Ryan's Railscast #197 in a system with Questions, Answers, and (multiple choice) Options. http://railscasts.com/episodes/197-nested-model-form-part-2.

  • I have successfully implemented the nesting among these forms/partials.
  • The simpler 'check box' way to delete records works properly.
  • The problem occurs when I try to add/delete records.

I have copied the code exactly as it appears in his Railscast:

#new.html.erb
<%= javascript_include_tag :defaults, :cache => true %>
<% f.fields_for :in_options do |builder| %>
  <%= render "option_fields", :f => builder %>
<% end %>

#_option_fields.html.erb partial
<%= f.hidden_field :_destroy %>
<%= link_to_function "remove", "remove_fields(this)" %>

#application_helper.rb (exact same as #197)
  def link_to_remove_fields(name, f)
    f.hidden_field(:_destroy) + link_to_function(name, "remove_fields(this)")
  end

  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

#application.js (exact same as #197. I have an Event.addbehavior below this code.)
function remove_fields(link) {
  $(link).previous("input[type=hidden]").value = "1";
  $(link).up(".fields").hide();  
}

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)
  });
}

2 problems:

  • When I click on the 'remove' link it doesn't remove - it just shifts the page up or down.
  • When I include link_to_add_fields "Add Answer", f, :answers, I get undefined method `klass' for nil:NilClass.

------PROGRESS------

If I move function remove_fields(link) to the top of new.html.erb, the remove link works. Which means, I have a problem accessing the function in my application.js. Here's my condensed structure.

# layout forms.html.erb
<html>
  <head>    
    <%= stylesheet_link_tag "global", "forms", "candidateCreateProfile", "LiveValidation", "formsAccount", :cache => true %>
    <%= javascript_include_tag :defaults, "LiveValidation" %>
  </head>
  <body>
    <%= yield %>
  </body>
</html>

# new.html.erb
<%= stylesheet_link_tag "interviewQuestions" %>
<%= javascript_include_tag "effects", "lowpro", "toggle", :cache => true %>
...#everything else, including my call to remove_fields
sscirrus
  • 55,407
  • 41
  • 135
  • 228
  • here is the solution: http://stackoverflow.com/questions/8895193/rails-3-and-jquery-railscast-197-issues – suely Mar 05 '12 at 18:15

5 Answers5

3

For those who still have undefined method `klass' for nil:NilClass issue see the following thread — has_many :through nested_form that can build multiple instances

Community
  • 1
  • 1
jibiel
  • 8,175
  • 7
  • 51
  • 74
1

Just stumpled upon this question - if it helps, use this awesome nested form gem by Ryan Bates.

Here's the railscast.

Dennis Hackethal
  • 13,662
  • 12
  • 66
  • 115
0

In my case the javascript wasn´t called so, I change the request for:

link_to name, '#', onclick: "add_fields(this,'#{association}','#{escape_javascript(fields)}')";
0

You are using a prototype library syntax instead of jQuery. Visit the episode page, and look for the code used part where both prototype and jQuery syntaxes are provided.

alucillo36
  • 91
  • 1
  • 10
0

Have you made sure app...js is being included in the output? You may need to edit your layout.

thomasfedb
  • 5,990
  • 2
  • 37
  • 65
  • I have <%= stylesheet_include_tag :defaults %> in my layout, which is called 'forms'. application.js is stored in /public/javascripts. – sscirrus Jun 18 '10 at 18:37
  • open up the page source and check it's being included, you might be using the wrong layout for that controller/action ? – thomasfedb Jun 19 '10 at 07:46