Follow Along as I Stumble on the Path to Learning RoR

Posts tagged “default

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
end

To edit an existing field:

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

Advertisements

Select Default

To have a default value above the options in a Rails select box, use :prompt.

ie

<%= f.select(:field_name, {“option1″=>”option1″ ,”option2″ =>”option2″}, :prompt=>”Please select an option”) %>


Time Zones in Rails 3

I was looking for what options I could use to set the time zone config in application.rb.  The best I could find were these posts in Stack Overflow:

http://stackoverflow.com/questions/5073917/why-doesnt-config-time-zone-seem-to-do-anything

http://stackoverflow.com/questions/4362663/timezone-with-rails-3

essentially they are (for the biggest US zones):

Pacific Time (US & Canada), Central Time (US & Canada), Eastern Time (US & Canada)

The posts are good to reference nonetheless, as there seems to be a lot of confusion on what the config does (and time helpers).