0
votes

I am new to rails and using a combination of formtastic, activeadmin,sti and polymorphic associations to build a form

When I I can create a nested form with the address parent with no problem, but when i introduce STI and attempt to build_origin_address instead of build_address, that is when I get the error below when loading the edit view

NameError in Admin/leads#edit Showing .../app/views/admin/leads/_form.erb where line #3 raised: uninitialized constant Lead::OriginAddress

Models:

class Address < ActiveRecord::Base
  belongs_to :addressable, :polymorphic => true
  belongs_to :lead
  validates :line1, :presence => true, :length =>  {:minimum => 2}
  attr_accessible :line1, :line2, :city, :state, :zip, :country
end

class OriginAddress < Address
end

class DestinationAddress < Address
end

class Lead < ActiveRecord::Base
  has_one  :origin_address, :dependent => :destroy, :as => :addressable
  accepts_nested_attributes_for :origin_address, :allow_destroy => true
end

partial used in edit view:

<%= semantic_form_for [:admin, @lead] do |f| %>
<% @lead.build_origin_address unless @lead.origin_address %>
  <%= f.inputs :name => "Lead Info" do  %>
    <%= f.input :first_name %>
    <%= f.input :last_name %>
  <% end %>

  <%= f.semantic_fields_for :origin_address do |origin| %>
    <%= origin.inputs :name => "Origin Address" do  %>
      <%= origin.input :line1 %>
      ....
      <% end %>
  <% end %>

  <%= f.buttons do %>
    <%= f.commit_button %>
  <% end %>
<% end %>
1
does the code above show the entire Lead class definition? the error message suggests that it it is looking for OriginAddress to be namespaced somewhere within the Lead class but cant find it. Are you are referencing the OriginAddress class inside of the Lead class? If so, you can solve this by adding :: before the const name. i.e. ::OriginAddressOutlawAndy

1 Answers

0
votes

I think you must define @lead before your form.