【问题标题】:Validation with exclusion helper and localization使用排除助手和本地化进行验证
【发布时间】:2016-04-05 21:59:13
【问题描述】:
  • Ruby 2.3.0
  • Rails 4.2.6

我的应用程序有不同的语言,我正在使用“排除”帮助程序验证用户名属性。

到目前为止,一切都很好。但是我有很多条款要禁止我的模型变得非常丑陋。

#user.rb

validates :username, exclusion: { in: :reserved_words }

def reserved_words
    %w(word1 word2 palavra1 palavra2 ...)
end

有没有办法在带有语言环境的 yml 中添加这些保留字?

对如何进行或更好的方法有任何想法吗?

【问题讨论】:

    标签: ruby-on-rails validation ruby-on-rails-4 localization


    【解决方案1】:

    您可以为此使用UsersHelper

    UsersHelper

    module UsersHelper
    
      class << self
        def reserved_words
           %w(word1 word2 palavra1 palavra2 ...)
        end
      end
    
    end
    

    用户.rb

    #user.rb
    
    validates :username, exclusion: { in: :UsersHelper.reserved_words }
    

    【讨论】:

    • 不错!谢谢,但我如何使用本地化?
    猜你喜欢
    • 1970-01-01
    • 2012-06-10
    • 1970-01-01
    • 1970-01-01
    • 2013-01-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-12
    相关资源
    最近更新 更多