2

Hi all I followed Ryan Bates' railscasts on nested models and form, but I am getting getting undefined method `klass' for nil:nilclass. I am pretty sure it is due to the the link_to_add_fields since everything was working prior. Below is my error and other relevant code and I'm using Rails 3.1. I did a lot of googling and did not find any to solve my problem, so if you guys could help me out I would really appreciated it. Thanks for your help.

_form.html.erb

<%= form_for(@organization) do |f| %>
  <% if @organization.errors.any? %>
    <div id="error_explanation">
      <h2><%= pluralize(@organization.errors.count, "error") %> prohibited this organization from being saved:</h2>

      <ul>
      <% @organization.errors.full_messages.each do |msg| %>
        <li><%= msg %></li>
      <% end %>
      </ul>
    </div>
  <% end %>

  <div class="field">
    <%= f.label :name %><br />
    <%= f.text_field :name %>
  </div>
    <div id="restaurant_field" class="field">
        <%= f.fields_for :restaurants do |builder| %>
            <%= render 'organizations/partials/restaurant_fields', :f => builder %>
        <% end %>
    </div>

  <div class="actions"><%= f.submit %></div>
<% end %>

_restaurant_fields.html.erb

<p class="fields">
<%= f.label :name, "Restaurant Name" %><br />
<%= f.text_field :name %>
<%= link_to_remove_fields "Remove", f %>

application_helper.rb

    module ApplicationHelper
  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
end

application.js

function remove_fields(link) {  
    $(link).prev("input[type=hidden]").val("1");  
    $(link).closest(".fields").hide();  
}  

function add_fields(link, association, content) {  
    var new_id = new Date().getTime();  
    var regexp = new RegExp("new_" + association, "g");  
    $(link).parent().before(content.replace(regexp, new_id));  
}
Pan Wangperawong
  • 1,250
  • 2
  • 13
  • 29

2 Answers2

2

I found that the link_to_add_fields helper won't work if the associated model is describe by a has_one. The has_one means the association does not get the klass object.

You can determine this is your problem by changing your relationship to has_many :object + s (your object name with an s) and passing your object in plural to link_to_add_fields.

michaelhawkins
  • 1,026
  • 8
  • 8
1

You have done the same thing as this person here

Edit: (error on my part)

I am assuming you have an link_to_add_fields below this line

<%= link_to_remove_fields "Remove", f %>

as it seems that the _restaurant_fields.html.erb partial is incomplete. (no closing tag)

</p>

Remove the link_to_add_fields outside of the f.fields_for

That should solve the klass error.

Community
  • 1
  • 1
Seanland
  • 136
  • 6