“.save” only inserts null values in database

前端 未结 3 989
时光说笑
时光说笑 2021-01-15 08:14

I\'m trying to make a RoR application for a Hospital so it has patients, doctors, offices, etc.

The problem I\'m having is that, at the patient \"Sign-up\", I\'m not

3条回答
  •  别那么骄傲
    2021-01-15 08:28

    So in your controller your params are nil but you call .to_s and .to_s.to_i which results in an empty string "" and 0 (zero). You then save them into your database. A couple recommendations:

    def pat_create
    
      pat = Patient.new(:name => params[:name], :pid =>params[:pid])
      pat.save
    
    end
    

    In addition to the uniqueness validation I would make sure your db column has a unique index on it to insure no duplicate patients.

    class Patient < ActiveRecord::Base
    
      attr_accessible :name, :pid
      attr_accessor :name, :pid
    
      has_many :appointments
      validates :name, presence: true, 
                       length: { maximum: 50 }, 
                       format: {:with=> /^[a-zA-Z\s\D]+$/}
      validates :pid, presence: true,
                      numericality: { only_integer: true, 
                                      greater_than_or_equal_to: 100000, 
                                      less_than_or_equal_to: 9999999999 },
                      uniqueness: true
    end
    

    If you are getting valid values this will work and if not you will see why it is not.

提交回复
热议问题