使用 Formtastic、STI、Polymorphic & 编辑表单加载错误活动管理员

发布于 2024-12-25 19:37:31 字数 1466 浏览 1 评论 0原文

我是rails新手,使用formtastic、activeadmin、sti和多态关联的组合来构建表单

当我可以毫无问题地使用地址父级创建嵌套表单时,但是当我引入STI并尝试build_origin_address而不是build_address时, 加载编辑视图 NameError 时收到以下错误

那是当我在 Admin/leads#edit 中 显示 .../app/views/admin/leads/_form.erb 其中第 3 行提出: 未初始化常量 Lead::OriginAddress

模型:

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

在编辑视图中部分使用:

<%= 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 %>

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

镜花水月 2025-01-01 19:37:31

我认为您必须在表单之前定义@lead。

I think you must define @lead before your form.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文