A. Singla A. Singla - 1 year ago 61
Ruby Question

Display 5 Digit Zip in Devise

I have a devise user model, and part of the information I ask of them is their 5 digit zip code. If their zip code is 08601, then, when they go to edit their user settings, they see their zip code as 8601, without the 0 in front. Is there a way to format the zip code that devise displays in the account settings (something like "%05d")? Here is what I have in that field so far:

<div class="form-group">
<%= f.label :zip_code %><br />
<%= f.text_field :zip_code, class: "form-control", autofocus: true %>


Answer Source

Problem might be in your database column data type

Since you are using ZIP code in your form I think you have used zip_code column in database as integer that's why you are getting this. Change data type of column to string and then try saving your data.

If you want to keep data type integer

If you do so you need to change your zip_code to string everytime you use it and add 00 before zip_code. This is not a good approach of programming and may be the problem later.

But You can do this in model

def self.zip_code_string
   @zip = self.zip_code.to_s

And to display or use


will work