gpt4 book ai didi

ruby-on-rails - Rails 将验证放在模块混合中?

转载 作者:数据小太阳 更新时间:2023-10-29 06:26:17 25 4
gpt4 key购买 nike

一些验证在我的模型中是重复的:

validates :name, :length => { :minimum => 2 }, :presence => true, :uniqueness => true
validates :name_seo, :length => { :minimum => 2 }, :presence => true, :uniqueness => true

我如何将其放入混音中?如果我只是将它们放入 mixin 中,我会收到此错误

app/models/validations.rb:5: undefined method `validates' for Validations:Module (NoMethodError)

最佳答案

module Validations
extend ActiveSupport::Concern

included do
validates :name, :length => { :minimum => 2 }, :presence => true, :uniqueness => true
validates :name_seo, :length => { :minimum => 2 }, :presence => true, :uniqueness => true
end
end

validates 宏必须在包含程序的上下文中进行评估,而不是在模块的上下文中进行评估(就像您可能正在做的那样)。

关于ruby-on-rails - Rails 将验证放在模块混合中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11372484/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com