Forum: Ruby on Rails Phone number not coming through: noob

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
39025a7f572d6fe464e1fe6be399ca3b?d=identicon&s=25 Mike Dershowitz (mdersh53)
on 2008-10-25 17:20
(Received via mailing list)
Hi:

used scaffold to create a phone number table.  But when phone number
saves it's not coming through as the number entered in the text field:

RESULT FROM DB WHEN I ENTER PHONE NUMBER OF 215 555 1212 INTO WEB
FORM:

REPLACE INTO "phones" ("id", "number", "phone_type_id", "employee_id",
"created_at", "updated_at") VALUES
  (13,2147483647,1,'8','2008-10-25 11:05:48','2008-10-25 11:05:48');

Seems like I'm doing something wrong that the phone number entered
insn't coming through as the number.  the "2147483647" value is the
same value that is entered in the DB all the time.

What am I doing wrong?  Thanks so much in advance!

All code below:

VIEW:

<% form_for(@phone) do |f| %>
<table width="600" border="0">
    <tr>
        <td class="label">Number:
        </td>
        <td><%= f.text_field :number %> (required - no spaces or
dashes)
        </td>
    </tr>
    <tr>
        <td class="label">Phone type:
        </td>
        <td><%= f.collection_select(:phone_type_id,
PhoneType.find(:all), :id, :name) %> (required)
        </td>
    </tr>
    <tr>
        <td class="label">Number:
        </td>
        <td><%= text_field_tag :number2 %> (required - no spaces or
dashes)
        </td>
    </tr>

<%= hidden_field_tag :employee_id, @phone.employee_id %>
    <tr >
        <td class="button form-right" align="right">
          <%= link_to "Cancel", "/dashboard.html" %>
        </td>
        <td class="button form-left"><%= f.submit "Finish >>" %>
        </td>
    </tr>
</table>
<% end %>

CONTROLLER:

 def new
    @phone = Phone.new
    if params[:id].nil?
    else
      @emp = Employees.find(params[:id])
      @phone.employee_id = params[:id]
    end

    respond_to do |format|
      format.html # new.html.erb
      format.xml  { render :xml => @phone }
    end
  end

  def create
    @phone = Phone.new(params[:phone])
    @phone.employee_id = params[:employee_id]

    respond_to do |format|
      if @phone.save
        flash[:notice] = 'Phone was successfully created.'
        format.html { redirect_to(@phone) }
        format.xml  { render :xml => @phone, :status
=> :created, :location => @phone }
      else
        format.html { render :action => "new" }
        format.xml  { render :xml => @phone.errors, :status
=> :unprocessable_entity }
      end
    end
  end

MODEL:

class Phone < ActiveRecord::Base
  has_many :phone_types
  belongs_to :employees


  validates_presence_of :number
  validates_length_of :number, :maximum => 10
  validates_numericality_of :number,
                            :message => "Phone number can be only
numbers."
end

DB MIGRATION:

class CreatePhones < ActiveRecord::Migration
  def self.up
    create_table :phones do |t|
      t.integer :number
      t.integer :phone_type_id
      t.integer :employee_id

      t.timestamps
    end
  end

  def self.down
    drop_table :phones
  end
end
81b61875e41eaa58887543635d556fca?d=identicon&s=25 Frederick Cheung (Guest)
on 2008-10-25 17:38
(Received via mailing list)
On 25 Oct 2008, at 16:19, Miked wrote:

>
> Hi:
>
> used scaffold to create a phone number table.  But when phone number
> saves it's not coming through as the number entered in the text field:
>
> RESULT FROM DB WHEN I ENTER PHONE NUMBER OF 215 555 1212 INTO WEB
> FORM:

You've created phone numbers as integer columns. by default the
database is probably using 32 bits integers which
2155551212 will overflow and the database stores the maximum possible
value for such a column instead.
You could change the column to a larger integer type, but if i were
you i would change it to a string column and phone numbers are numbers
in the usual sense (eg the leading 0 in a phone number is significant,
but an integer column won't store that)

Fred
This topic is locked and can not be replied to.