【问题标题】:Rails Polymorphic Association, causing 'nil is not a symbol' TypeError from form_for when trying to create newRails 多态关联,在尝试创建新时导致来自 form_for 的“nil 不是符号”TypeError
【发布时间】:2011-10-12 20:02:41
【问题描述】:

用户模型

belongs_to :role, :polymorphic => true

admin_profile 模型(不是实际名称,但您明白了)

has_one :user, :as => :role

user_profile 模型(不是实际名称,但您明白了。除了这两个之外还有其他模型。)

has_one :user, :as => :role

当我访问 /admin_profile/new 时,我在第 1 行得到 'nil is not a symbol' TypeError。

<%= form_for(@admin_profile) do |f| %>
    ...

@admin_profile 的内容是:

<AdminProfile id: nil, birth_date: nil, url: nil>

轨迹是这样的

activerecord (3.1.1) lib/active_record/attribute_methods/primary_key.rb:8:in `to_key'
actionpack (3.1.1) lib/action_controller/record_identifier.rb:76:in `record_key_for_dom_id'
actionpack (3.1.1) lib/action_controller/record_identifier.rb:57:in `dom_id'
actionpack (3.1.1) lib/action_view/helpers/form_helper.rb:388:in `apply_form_for_options!'
actionpack (3.1.1) lib/action_view/helpers/form_helper.rb:365:in `form_for'

如果需要任何其他信息,请告诉我。

【问题讨论】:

  • 此错误通常是由拼写错误引起的。仔细检查所有代码。

标签: ruby-on-rails activerecord polymorphic-associations view-helpers


【解决方案1】:

问题是一些表有 :id => false。让这成为 db:schema:dump 用户的警告

【讨论】:

  • 我遇到了同样的问题。奇怪的。这似乎不是一个持续存在的问题(只是删除数据库并运行迁移可以很好地解决它),但我想我只是插话以防万一其他善良的灵魂想要进一步研究它。 FWIW,我在 psql 上。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-02-04
  • 1970-01-01
  • 2014-12-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多