0

I've added two news fields to my users table :first_name and :last_name

In migration file I've specified these fields couldn't be null using the following code:

class Userscustomfields < ActiveRecord::Migration
  def change
    add_column :users, :first_name, :string, {:null => false, :limit => "128"}
    add_column :users, :last_name, :string, {:null => false, :limit => "128"}
  end
end

I've checked on my database and the table was configurated correctly. But when I'm going to do a new register, my rails app allows to me to add a new user with first and last names empty. The app saves the fields with "", which didn't worked as I expected.

How can I do prevent this, by making a check of emptiness on my field before save them on my database?

4

1 回答 1

0

我穿上模型 user.rb

validates :first_name, :last_name, presence: true  
于 2013-11-12T11:11:48.460 回答