Setting Default Values for Rails Fields

For some fields, you may want to set default values for when a new record is created.  For example, if you want to set a flag/boolean field to true or false when a new record is created.  An easy way to do this is through a migration.

If you are just creating the field, you can do:

def change
add_column :table_name, :field_name, :field_type, :default => 0
add_column :table_name, :field_name, :field_type, :default => true

To edit an existing field:

def up
change_column :table_name, :field_name, :boolean, :default => true


Default Values and Placeholders in Text Fields

You can place text in fields to pre-populate them or to save space by putting the label in the field with simple html.

If you want label a field, you can use the placeholder tag which works in most modern browsers:

If you want to pre-populate a field, with text, you can use the value tag which will put the text, but NOT remove it when the focus is put on the field: