5 thoughts on “Prevenire la registrazione di login riservate

  1. Justin Baker

    whoops, forgot to highlight code

    1
    2
    3
    4
    5
    6
    7
    8
    class User < ActiveRecord::Base
      RESERVED = %w{admin root administrator superuser}.collect{|n| [n,n.capitalize]}.flatten

    validates_exclusion_of :login,
        :in => RESERVED,
        :message => "is reserved"

    end
  2. Pete Roome

    Nice post, thanks for the heads up. For some reason i had’nt really considered this into the build of my app.

  3. Pixoo

    Your method could also be written like this:

    1
    2
    3
    4
    5
    def excluded_login
      if %w( admin root administrator superuser ).include?(user.login.downcase)
        user.errors.add(:login, "is reserved")
      end
    end

Comments are closed.