I am trying to implement Ryan Railscast # 197 on a system with parameters for questions, answers, and (multiple choice). http://railscasts.com/episodes/197-nested-model-form-part-2 .
- I have successfully implemented nesting between these forms / particles.
- The simplest way to delete entries using the checkbox works correctly.
- The problem occurs when I try to add / remove entries.
I copied the code exactly as it appeared in its Railscast:
<%= javascript_include_tag :defaults, :cache => true %>
<% f.fields_for :in_options do |builder| %>
<%= render "option_fields", :f => builder %>
<% end %>
<%= f.hidden_field :_destroy %>
<%= link_to_function "remove", "remove_fields(this)" %>
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
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 the delete link, it is not deleted - it just moves the page up or down.
- When I turn on link_to_add_fields "Add an answer", f :: answers, I get the undefined `klass' method for nil: NilClass.
------ PROGRESS ------
remove_fields (link) new.html.erb, . , application.js. .
<html>
<head>
<%= stylesheet_link_tag "global", "forms", "candidateCreateProfile", "LiveValidation", "formsAccount", :cache => true %>
<%= javascript_include_tag :defaults, "LiveValidation" %>
</head>
<body>
<%= yield %>
</body>
</html>
<%= stylesheet_link_tag "interviewQuestions" %>
<%= javascript_include_tag "effects", "lowpro", "toggle", :cache => true %>
...