NoMethodError:Service的未定义方法set_password(调用“ Service.connection”以建立连接):类

问题描述 投票:0回答:1

为什么会出现此错误?

NoMethodError: undefined method `set_password' for Service (call 'Service.connection' to establish a connection):Class

这是我的模型(命名为Service)

class Service < ApplicationRecord
    def set_password
        self.password = p SecureRandom.base64(5)
        self.save
    end
end

感谢您的回答。

ruby-on-rails ruby nomethoderror
1个回答
0
投票

您必须在类类(例如Service.set_password)而不是实例(例如Service.new.set_password或Service.first.set_password)上调用方法。

阅读类和实例方法之间的区别:

http://ruby-for-beginners.rubymonstas.org/writing_classes/methods.html

© www.soinside.com 2019 - 2024. All rights reserved.