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| %>
Number: | <%= f.text_field :number %> (required - no spaces or dashes) |
Phone type: | <%= f.collection_select(:phone_type_id, PhoneType.find(:all), :id, :name) %> (required) |
Number: | <%= text_field_tag :number2 %> (required - no spaces or dashes) |
<%= link_to “Cancel”, “/dashboard.html” %> |
<%= f.submit “Finish >>” %> |
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