【问题标题】:unknown attribute with devise具有设计的未知属性
【发布时间】:2011-07-03 00:37:21
【问题描述】:

对于我的数据模型,我有两种不同的类型:家庭成员和朋友。我的计划是让每个人都有一个指向由 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, :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 和其他任何东西添加到两个模型中。我可以使用表继承,但我不想这样做(除非我可以将外键添加到指向超类的子类,那很好)。

【问题讨论】:

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


    【解决方案1】:

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

    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
    

    【讨论】:

      【解决方案2】:

      尝试将此添加到您的 Friend 模型中:

      attr_accessible :username, :email,  :password, :password_confirmation, :remember_me
      

      您是否尝试过这样做:

      class User < ActiveRecord::Base
        devise :database_authenticatable, :registerable,
               :recoverable, :rememberable, :trackable, :validatable
      end
      
      class Friend < User
        belongs_to :label
        belongs_to :user
        belongs_to :gender
        belongs_to :location
        belongs_to :orientation
        belongs_to :matchmaker
      end
      
      class FamilyMember < User
      end
      

      【讨论】:

      • 我的代码中确实有它,我只是做了一个糟糕的复制粘贴工作。
      【解决方案3】:

      您确定在用户迁移中添加了“用户名”字段吗?设计不会自动添加。

      【讨论】:

      • 顺便说一句,设计方面可能最好将所有设计逻辑保留在用户模型中并更新它,以便它拥有一个或属于朋友/家庭成员 - 但这当然取决于情况:)
      猜你喜欢
      • 2015-11-28
      • 1970-01-01
      • 2017-08-23
      • 2017-10-13
      • 2021-03-08
      • 1970-01-01
      • 2015-11-09
      • 2017-07-17
      • 2011-02-27
      相关资源
      最近更新 更多