如何使用非拆分属性填充虚拟表单字段

时间:2013-04-21 19:58:27

标签: ruby-on-rails forms getter-setter virtual-attribute

我有一个包含3个虚拟属性(phoneAreaCode,phonePrefix和phoneSuffix)的表单,将一个电话号码拆分成多个段,这些段连接在一起并以phoneNum的形式保存到数据库中。

当用户更新其个人资料时,大多数字段都会填写,但虚拟字段不会填写。我尝试使用我认为是吸气剂来使其工作,但我无法弄清楚我错过了什么。在那个注释中,我使用的是具有第二个模型的地址属性的嵌套表单,但是,当这些属性为nil时,表单字段根本不会显示在更新视图中。即使它们是零,有没有办法让这些显示?嵌套表单属性显示在注册视图中,注册和更新视图都从布局呈现相同的表单。该模型如下。

class User < ActiveRecord::Base
  attr_accessor :phoneAreaCode, :phonePrefix, :phoneSuffix

  attr_accessible :MOS, :dateOfBirth, :ets_pcsDate, :firstName, 
  :lastName, :middleInitial, :phoneNum, :phoneAreaCode, :phonePrefix,
  :phoneSuffix, :rank, :email, :password, :password_confirmation, 
  :address_attributes

  has_secure_password
  has_one :address, dependent: :destroy

  accepts_nested_attributes_for :address


  before_save {  |user| user.email = email.downcase  }
  before_save :create_remember_token
  before_save :create_phoneNum

  validates :rank,      presence: true
  validates :firstName,     presence: true, length: {  maximum: 15  }
  validates :lastName,  presence: true, length: {  maximum: 20  }
  validates :middleInitial, presence: true, length: {  maximum: 1  }

  VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i
  validates :email,presence: true,  format: {  with: VALID_EMAIL_REGEX  },
                        uniqueness: {  case_sensitive: false  }

  validates :dateOfBirth,    presence: true
  VALID_MOS_REGEX = /\A\d{2}[a-zA-Z]\z/
  validates :MOS,           presence: true, 
                                    format: {  with: VALID_MOS_REGEX }
  validates :ets_pcsDate,       presence: true

  VALIDATE_AREA_PREFIX_REGEX = /\A\d{3}\z/
  validates :phoneAreaCode,     presence: true,  
                                    format: { with: VALIDATE_AREA_PREFIX_REGEX }                               
  validates :phonePrefix,           presence: true,
                                    format: { with: VALIDATE_AREA_PREFIX_REGEX }
  VALIDATE_SUFFIX_REGEX = /\A\d{4}\z/                                       
  validates :phoneSuffix,           presence: true,
                                    format: { with: VALIDATE_SUFFIX_REGEX }
  validates :password,      length: {  minimum: 6  }
  validates :password_confirmation, presence: true


  private
    def create_phoneNum
      self.phoneNum = [phoneAreaCode, 
                      phonePrefix, phoneSuffix].join(' ')
    end

    def create_phoneNum=(phoneNum)
      split = phoneNum.split(' ', 3)
      self.phoneAreaCode = split.first
      self.phonePrefix = split.second
      self.phoneSuffix = split.last
    end

    def create_remember_token
      self.remember_token = SecureRandom.urlsafe_base64
    end
end

2 个答案:

答案 0 :(得分:0)

据我了解,你只坚持'phoneNum'。您可以将attr_accessor重命名为attr_writer并手动设置getter。

def phoneAreaCode
  # Get phone area code
end

def phonePrefix
  # Get phone prefix
end

def phoneSuffix
  # Get phone suffix
end

或者如果您真的想使用该方法 - 您可以这样做:

after_initialize :create_phoneNum

但我会保证第一种方法。

答案 1 :(得分:0)

使用

def phoneAreaCode
  if phoneNum != nil
     @phoneAreaCode ||= self.phoneNum.split(' ')[0]
  else
     @phoneAreaCode
  end
end

def phonePrefix
  if phoneNum != nil
    @phonePrefix ||= self.phoneNum.split(' ')[1]
  else
    @phonePrefix
  end
end

def phoneSuffix
  if phoneNum != nil
    @phoneSuffix ||= self.phoneNum.split(' ')[2]
  else
    @phoneSuffix
  end
end