简体   繁体   English

通过多态设计和属具属的accepts_nested_attributes_for

[英]accepts_nested_attributes_for with belongs_to polymorphic devise

I have 3 models and this associations for them 我有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

I'm out of the box from rails way to put accepts_nested_attributes_for :rolable on user model and In this accepts_nested_attributes_for with belongs_to polymorphic question I found some solutions for it but all solution not works for me. 我开箱即用的方式将accepts_nested_attributes_for :rolable放在用户模型上,在这个带有accepts_nested_attributes_for的多态问题中,我找到了一些解决方案,但是所有解决方案都不适合我。 All solutions, always the same error when I try to create a user 所有解决方案,在尝试创建用户时始终出现相同的错误

    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'

My mistake, I'm use nested form 我的错误,我使用嵌套表格

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

change to 改成

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

for polymorphic association you have to maintain generic model Roll like 对于polymorphic关联,您必须维护通用模型Roll

class User < ActiveRecord::Base
  # devise modules here
  attr_accessible :email, :password, :password_confirmation, :remember_me
  has_many :rolls, :as => :rolable
end

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

class Manager < ActiveRecord::Base
  attr_accessible :age, :name
  has_many :rolls, :as => :rolable
end

class Roll < ActiveRecord::Base
  attr_accessible :rolable_id, :rolable_type
  belongs_to :rolable, :polymorphic=> true
end

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 accepted_toted_attributes_for with belongs_to polymorphic - accepts_nested_attributes_for with belongs_to polymorphic Rails:belongs_to和accepts_nested_attributes_for,多态 - Rails: belongs_to & accepts_nested_attributes_for, polymorphic RecordNotFound 与 accepts_nested_attributes_for 和belongs_to - RecordNotFound with accepts_nested_attributes_for and belongs_to 如何在belongs_to关系中正确设置accepts_nested_attributes_for - How to properly setup accepts_nested_attributes_for in belongs_to relationship 不使用设计保存的属性和 accepts_nested_attributes_for - Attributes not saving with devise and accepts_nested_attributes_for accepts_nested_attributes_for - belongs_to,has_many,fields_for - accepts_nested_attributes_for - belongs_to, has_many, fields_for 获取fields_for和accepts_nested_attributes_for以使用belongs_to关系 - Getting fields_for and accepts_nested_attributes_for to work with a belongs_to relationship 在belongs_to模型activeadmin资源中,accepts_nested_attributes_for的替代方法是什么? - What is the alternative for accepts_nested_attributes_for in the belongs_to model activeadmin resource? 模型上的多个belongs_to导致父类的accepts_nested_attributes_for错误 - multiple belongs_to on model causing errors with accepts_nested_attributes_for on parent 具有多态关联的accepts_nested_attributes_for - accepts_nested_attributes_for with polymorphic association
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM