ruby – 轨道模型的“未定义方法”
发布时间:2020-12-17 03:53:16 所属栏目:百科 来源:网络整理
导读:我正在使用带有rails的Devise,我想添加一个方法“getAllComments”,所以我写这个: class User ActiveRecord::Base # Include default devise modules. Others available are: # :token_authenticatable,:encryptable,:confirmable,:lockable,:timeoutable a
我正在使用带有rails的Devise,我想添加一个方法“getAllComments”,所以我写这个:
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 :email,:password,:password_confirmation,:remember_me,:city,:newsletter_register,:birthday,:postal_code,:address_complement,:address,:lastname,:firstname,:civility has_many :hotels_comments class << self # Class methods def getAllComments true end end end 在我的控制器中: def dashboard @user = current_user @comments = @user.getAllComments(); end 当我去我的网址时,我得到了 undefined method `getAllComments' for #<User:0x00000008759718> 我做错了什么? 谢谢 解决方法
因为getAllComments是一个类方法,并且您尝试将其作为实例方法进行访问.
您需要访问它: User.getAllComments 或者将其重新定义为实例方法: class User < ActiveRecord::Base #... def getAllComments true end end def dashboard @user = current_user @comments = @user.getAllComments end (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |