【问题标题】:Formtastic form throws "undefined method" error with has_one associationFormtastic 表单使用 has_one 关联引发“未定义方法”错误
【发布时间】:2011-11-04 20:58:35
【问题描述】:

我正在使用 Rails 3.1 和 Ruby 1.9.2 以及 Active Admin 来构建 CMS。这是我的 Place 和 Image 模型:

class Place < ActiveRecord::Base
  has_one :image    
  accepts_nested_attributes_for :image
end

class Image < ActiveRecord::Base
  belongs_to :place 
end

这是我在 Places 控制器的“新”操作中呈现的 Formtastic 表单:

<%= semantic_form_for [:admin, @place] do |p| %>
  <%= p.inputs "Details" do %>
    <%= p.input :name %>
    <%= p.input :description %>
    <%= p.input :phone %>
    <%= p.input :address %>
    <%= p.input :image %>
  <% end %>
  <%= p.buttons %>
<% end %>

当我在浏览器中加载表单时,我看到以下错误:

undefined method `place_id' for #<Place:0xb801744>

这是关键:在我的 Place 模型中,如果我将 has_one :image 更改为 has_many :imagesaccepts_nested_attributes_for :imageaccepts_nested_attributes_for :images,并且在我的表单中我将 p.input :image 更改为 p.input :images,那么错误就会消失并且Formtastic 正确呈现包含所有可用图像对象的多选输入元素。那么,当我使用 has_one 关联而不是看到选择输入元素时,为什么会出现此错误?

【问题讨论】:

  • Place 是否有 image_id 或 Image 是否有 place_id。如果后者正在工作,我猜 Image 有一个 place_id,如果是这样,请尝试将 image_id 放在 Place 上。
  • 自从一个 Place has_one :image,Images 表有一个 Places 表的外键。
  • true - 但由于约定的应用出现错误,即为什么它在 Place 中寻找 place_id,我认为它可能值得一试。
  • 似乎与stackoverflow.com/questions/7833554/… 相关(截至目前仍未得到答复)

标签: activerecord ruby-on-rails-3.1 formtastic activeadmin


【解决方案1】:

根据https://github.com/gregbell/active_admin/issues/575,这是 formtastic 和活动管理员之间的错误 - 我建议您在 github 上关注该问题并向他们解释您的情况,如果多个社区成员报告该问题,它可能会提示修复。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多