gpt4 book ai didi

ruby-on-rails - accepts_nested_attributes_for 和belongs_to 多态设计

转载 作者:行者123 更新时间:2023-12-04 19:06:01 26 4
gpt4 key购买 nike

我有 3 个模型和它们的关联

class User < ActiveRecord::Base
# devise modules here
attr_accessible :email, :password, :password_confirmation, :remember_me, :rolable_id, :rolable_type
belongs_to :rolable, :polymorphic => true
end

class Player < ActiveRecord::Base
attr_accessible :age, :name, :position
has_one :user, :as => :rolable
end

class Manager < ActiveRecord::Base
attr_accessible :age, :name
has_one :user, :as => :rolable
end

我开箱即用,从 rails 的方式放入 accepts_nested_attributes_for :rolable关于用户模型和在此 accepts_nested_attributes_for with belongs_to polymorphic问题我找到了一些解决方案,但所有解决方案都不适合我。所有解决方案,当我尝试创建用户时总是出现相同的错误
    Processing by RegistrationsController#create as HTML
Parameters: {"utf8"=>"V", "authenticity_token"=>"WKCniJza+PS5umMWCqvxFCZaRVQMPZBT4nU2fl994cU=", "user"=>{"email"=>"john@email.com", "password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]", "rolable_type"=>"manager", "rolable"=>{"name"=>"john", "age"=>"24"}}, "commit"=>"Sign up"}
Completed 500 Internal Server Error in 143.0ms

NoMethodError (undefined method `primary_key' for ActiveSupport::HashWithIndifferentAccess:Class):
app/controllers/registrations_controller.rb:13:in `new'
app/controllers/registrations_controller.rb:13:in `create'

最佳答案

我的错误,我使用的是嵌套表单

<%= f.fields_for :rolable do |rf| %>
....
<% end %>

改成
<%= f.fields_for :rolable_attributes do |rf| %>
....
<% end %>

关于ruby-on-rails - accepts_nested_attributes_for 和belongs_to 多态设计,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24277681/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com