开发者

Problem with Railscast #197 - Nested Model Form Part 2

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)" %>

#applicati开发者_如何学Goon_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


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


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

Here's the railscast.


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


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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜