2016-04-05 36 views
0
  • 的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中?

有關如何進行或更好的方法的任何想法?

回答

1

您可以使用UsersHelper

UsersHelper

module UsersHelper 

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

end 

User.rb

#user.rb 

validates :username, exclusion: { in: :UsersHelper.reserved_words } 
+0

不錯!謝謝,但我怎麼能使用本地化? – maiconsanson