最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Problem with Railscast #197 - Nested Model Form Part 2 - Stack Overflow

programmeradmin1浏览0评论

I'm trying to implement Ryan's Railscast #197 in a system with Questions, Answers, and (multiple choice) Options. .

  • 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

I'm trying to implement Ryan's Railscast #197 in a system with Questions, Answers, and (multiple choice) Options. http://railscasts./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
Share Improve this question edited Jun 18, 2010 at 19:19 sscirrus asked Jun 18, 2010 at 1:49 sscirrussscirrus 56.9k42 gold badges138 silver badges237 bronze badges 1
  • here is the solution: stackoverflow./questions/8895193/… – suely Commented Mar 5, 2012 at 18:15
Add a ment  | 

5 Answers 5

Reset to default 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

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)

  1. 暂无评论