【问题标题】:Cannot build has_one child object from nested form_for无法从嵌套 form_for 构建 has_one 子对象
【发布时间】:2011-11-21 06:16:04
【问题描述】:

我有一个用户模型,它有一个规范。在我的用户模型中,我确实指定了accepts_nested_attributes_for :spec 和attr_accessible :spec_attributes。

我为用户和规范创建了一个嵌套表单(嵌套)。发送到 #create 方法的哈希看起来很好:

 Parameters: {"utf8"=>"✓", "authenticity_token"=>"XXX", "user"=>{"first_name"=>"Adam", 
  "last_name"=>"Smith", "password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]", 
  "spec_attributes"=>{"highest_degree"=>""}}

但是,我遇到了无法建立关联规范的错误:

ArgumentError (Cannot build association spec. Are you trying to build a polymorphic 
one-to-one association?)

我的规范不是多态的。当我尝试在 User 对象上调用 build_spec 时,我收到一条错误消息,指出这是一条私人消息。我打开了这个错误的源代码,它说当用户没有响应方法 build_spec 时会出现这个消息错误。

我做错了什么,我该如何解决?

谢谢。

【问题讨论】:

  • 我认为您需要更多详细信息让任何人回答这个问题。
  • 同时命名模型规格可能是个坏主意。我相信它不会导致这个问题,但可能会导致很多其他问题。
  • 谢谢。我之前确实遇到过 Spec 的问题。但是在 Rails 中的重构很痛苦(我正在使用 textMate),所以我将不得不离开它一段时间。

标签: ruby-on-rails activerecord nested-forms has-one


【解决方案1】:

我想通了。我创建了一个名为 build_spec 的私有方法,因此覆盖了 build_association 的默认方法。

【讨论】:

    猜你喜欢
    • 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
    相关资源
    最近更新 更多