加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ruby-on-rails – 设计中的未知属性

发布时间:2020-12-17 02:11:25 所属栏目:百科 来源:网络整理
导读:对于我的数据模型,我有两种不同的类型:家庭成员和朋友.我的计划是每个都有一个用户表的外键,由Devise创建.因此,当用户注册时,我希望他们转到/ friends / sign_up或family_members / sign_up.所以,我的朋友课是 class Friend ActiveRecord::Base belongs_to
对于我的数据模型,我有两种不同的类型:家庭成员和朋友.我的计划是每个都有一个用户表的外键,由Devise创建.因此,当用户注册时,我希望他们转到/ friends / sign_up或family_members / sign_up.所以,我的朋友课是

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,:timeoutable and :omniauthable
#  devise :database_authenticatable,#         :recoverable,:validatable

  attr_accessor :username,:remember_me
  # Setup accessible (or protected) attributes for your model
  attr_accessible :username,: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添加到两个模型中.我可以使用表的inhertence,但我不愿意(除非我可以在指向超类的子类中添加一个外键,这样会很好).

解决方法

好的,我已经解决了我的问题.这是我的新代码供将来参考:

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,:validatable

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读