ruby-on-rails - 带有设计的未知属性

标签 ruby-on-rails ruby-on-rails-3 activerecord devise

对于我的数据模型,我有两种不同的类型:家人和 friend 。我的计划是让每个人都有一个指向由 Devise 创建的 User 表的外键。因此,当用户注册时,我希望他们转到/friends/sign_up 或 family_members/sign_up 。所以,我的 friend 类是

class Friend < ActiveRecord::Base
  belongs_to :label
  belongs_to :user
  belongs_to :gender
  belongs_to :location
  belongs_to :orientation
  belongs_to :matchmaker

  def after_initialize
    self.build_user if self.user.nil?
  end
  #accepts_nested_attributes_for :user
  delegate :username, :to => :user
  delegate :email, :to => :user
  delegate :password, :to => :user
  delegate :password_confirmation, :to => :user
  delegate :rememebr_me, :to => :user

  # Include default devise modules. Others available are:
  # :token_authenticatable, :encryptable, :confirmable, :lockable, :timeoutable and :omniauthable
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable
  # Setup accessible (or protected) attributes for your model
  attr_accessible :username, :email,  :password, :password_confirmation, :remember_me
end

我的用户类是
class User < ActiveRecord::Base
  # Include default devise modules. Others available are:
  # :token_authenticatable, :encryptable,     :confirmable, :lockable, :timeoutable and :omniauthable
#  devise :database_authenticatable, :registerable,
#         :recoverable, :rememberable, :trackable, :validatable

  attr_accessor :username, :email,    :password, :password_confirmation,   :remember_me
  # Setup accessible (or protected) attributes for your model
  attr_accessible :username, :email,   :password, :password_confirmation, :remember_me
end

我也在使用 Formtastic 作为我的观点。
现在,我得到
unknown attribute: username

带参数
{"utf8"=>"✓", "authenticity_token"=>"8ScsJebuCWnflaRQkp9MsBuaaqfzQKaZBXotLyNwNyM=",
"friend"=>{"username"=>"aaaa",
"email"=>"aaa@aaa.com",
"password"=>"[FILTERED]",
"password_confirmation"=>"[FILTERED]"},
"commit"=>"Create Friend"}

现在,我只是随机尝试向两个模型添加 nested_attributes 和任何内容。我可以使用表继承,但我不想这样做(除非我可以向指向父类(super class)的子类添加外键,否则就可以了)。

最佳答案

好的,我已经解决了我满意的问题。这是我的新代码以供将来引用:

class Friend < ActiveRecord::Base 
  belongs_to :friend_user, :class_name => 
end

class FriendUser < User
  set_table_name :users
  has_one :friend
end
class User < ActiveRecord::Base
  # Include default devise modules. Others available are:
  # :token_authenticatable, :encryptable,  :confirmable, :lockable, :timeoutable and :omniauthable
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable

  # Setup accessible (or protected) attributes for your model
  attr_accessible :username, :email,     :password, :password_confirmation, :remember_me
end

关于ruby-on-rails - 带有设计的未知属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6560393/

相关文章:

ruby-on-rails - Railstutorial.org Michael Hartl 第 9 章练习 3 "should not allow the admin attribute to be edited via the web"

javascript - Chart.js 气泡图更改数据集标签

ruby-on-rails - 我可以访问在 ruby​​ on Rails 中调用实例方法的集合吗

javascript - 在 Javascript 文件中使用 Ruby/Rails 代码

mysql - 计算 Rails 中的关联记录

ruby-on-rails - 引用 has_many (Rails) 中的实例

ruby-on-rails - Rails 4 和 Devise 的活跃工作

ruby-on-rails - 使用 Ransack 按全名搜索

ruby-on-rails-3 - Rails 3 Active Admin将预设值添加到新记录

jquery - 我应该如何正确刷新呈现部分的部分(通过js刷新会导致页面中的页面)