This project is archived and is in readonly mode.

#1610 ✓invalid

text_field eating leading zero strings

Reported by Gravis | December 20th, 2008 @ 10:28 PM | in 2.x


I have an issue where users must be able to set their postal code. We've set postal code to be a 5 length string. If we're using :

<%= form.text_field :postal_code, :size => 10 %>

The postal code is always displayed with leading zeros missing, even if the value is right in irb (user.postal_code => "02456" will be displayed "2456")

We changed our code to

<%= text_field_tag 'user[postal_code]', @user.postal_code, :size => 5, :maxlength => 5 %>

and now the leading zeros are there.

By the way, we had to override the method in user model to display the value correctly :

def postal_code

So, not sure if it's a real bug, or a ActionView nor ActiveRecord bug.

Thanks, Phil

Comments and changes to this ticket

  • Frederick Cheung

    Frederick Cheung December 21st, 2008 @ 04:39 PM

    That's weird - I tried this on edge and 2.2.2 and it seem to work fine. What version of rails were you using.

  • Gravis

    Gravis December 21st, 2008 @ 08:45 PM


    I'm using rails 2.2.2, and the site has been migrated several time from 1.X.

    The column type is a string, in schema.rb :

    t.string   "postal_code"

    and the problem occurs using sqlite and mysql.


  • Gravis

    Gravis December 21st, 2008 @ 11:36 PM

    can't reproduce problem on a fresh 2.2.2 app.

    Okkkk, really sorry, my fault, the field was indeed an integer, and not a string (we have 2 postal codes there).

    We can close the ticket, and really sorry for the time spend :(

  • Yehuda Katz (wycats)

    Yehuda Katz (wycats) December 24th, 2008 @ 08:12 AM

    • State changed from “new” to “invalid”

    Gravis: no need to apologize! Is there anything we can do in documentation to reduce the possibility of someone else experiencing this same issue?

  • Gravis

    Gravis December 24th, 2008 @ 10:18 AM

    Thanks Yehuda !

    I think just a little paragraph in the guides explain that fields with possible leading zeros like postal codes, phone numbers, etc should be stored as string (with a :limit if possible). Also, It's generally a good idea to store this large "numbers" in a string field, since we generally don't do any calculation on them, nor number comparison ("postal code > value" ?). I'll try to add some lines in the rails guides regarding that.

    Thanks Gravis (hope this post won't re-open the ticket !)

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile »

<h2 style="font-size: 14px">Tickets have moved to Github</h2>

The new ticket tracker is available at <a href=""></a>

People watching this ticket