I am a little new to the Ruby language and Rails framework but I really want to add some features to a registration page. Although this code works, I need to make it simpler and less cluttered but I am not sure where to begin here.
class User < ActiveRecord::Base
has_many :posts
has_many :comments
has_many :votes
has_secure_password validations: false
validates :username, presence: true, on: :create, length: {minimum: 5}, uniqueness: true
validates :username, presence: true, on: :update, length: {minimum: 5}, uniqueness: true
validates :password, presence: true, on: :create, length: {minimum: 5}
validates :password, presence: true, on: :update, length: {minimum: 5}
validates_format_of :username, on: :create, with: /\A[A-Za-z\d_]+\z/, message: "can only include letters and numbers"
validates_format_of :username, on: :update, with: /\A[A-Za-z\d_]+\z/, message: "can only include letters and numbers"
end
I want my users to be able to only include letters and numbers with no spaces for both their username and password. Also, they both need to be a minimum of 5 characters. Right now, only characters and numbers with no spaces works with my create action but it does not work for my update action. Any help would be greatly appreciated.