0

I have a form. One of the fields is zip code. If I fill out the form, here are the params:

 Parameters: {"utf8"=>"✓", "authenticity_token"=>"fasdafdsafdsdsa/MKI/RGfds3=", 
              "profile"=>{"title"=>"profile title", "zip_code"=>"02155"}}

As you can see, the zip code is still in tact.

I have a custom zip code validation:

VALID_ZIP_CODE_REGEX= /\A[0-9]{5}(-[0-9]{4})?\z/ #Does not work for +4
validates :zip_code, format: { :with => VALID_ZIP_CODE_REGEX }, 
   allow_blank: true, unless: 'dont_do_this?'

Here is the beginning of the method dont_do_this?

def dont_do_this
     puts 'self: ' + self.inspect.to_s
end

and here is the output:

self: #<Profile id: 7, created_at: "2014-05-06 16:50:14", updated_at: "2014-08-21 17:43:35",
         zip_code: 2155>

For some reason, the first zero of the zip code is stripped off. I can't figure out why. Does anyone know?

Philip7899
  • 4,599
  • 4
  • 55
  • 114
  • 3
    what is the data type of your zip_code in database? If it's integer then you should use string as for integer 0123 is equal to 123 – Mandeep Aug 21 '14 at 18:17

1 Answers1

0

It looks like you are saving ZIP codes as integers in the database. They should be stored as strings (or from the database's point of view, VARCHARS).

Saul
  • 911
  • 1
  • 8
  • 19