How do I make a drop down menu reflect what’s stored in it’s corresponding column in a database?
I have a dropdown menu for birthday selection and it updates the database fine
but goes back to default options in select menu on refresh where as all my text fields are pulling db data fine.
My form:
<%= form_for @profile, :remote => true, do |f| %>
Username: <%= @profile.user.username %><br />
URL: http://site.com/<%= @profile.user.username %><br />
First Name: <%= f.text_field :first_name, %><br />
Last Name: <%= f.text_field :last_name, %><br />
I am <%= f.select :gender, options_for_select([['Select Gender', ''],['Male','m'],['Female','f']], "#{@profile.gender if @profile.gender}") %><br />
My birthday:
<%= f.select :day, options_for_select(Profile::DAYS), :include_blank => "Day" %>
<%= f.select :month, options_for_select(Profile::MONTHS), :include_blank => "Month" %>
<%= f.select :year, options_for_select(Profile::YEAR_RANGE), :include_blank =>"Year" %><br />
<%= f.submit 'update' %><br />
<% end %>
Any clue what I’m missing?
Kind regards
Here’s my model:
class Profile < ActiveRecord::Base
belongs_to :user
attr_accessor :password, :day, :month, :year
attr_accessible :first_name, :last_name, :gender, :motd, :day, :month, :year
# Local Variables
# Regex Variables
email_regex = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i
alpha_regex = /^[a-zA-Z]*$/
alpha_numeric_regix = /^[a-zA-Z0-9_]*$/
MONTHS = ["January", 1], ["February", 2], ["March", 3], ["April", 4], ["May", 5], ["June", 6], ["July", 7], ["August", 8], ["September", 9], ["October", 10], ["November", 11], ["December", 12]
DAYS = ["01", 1], ["02", 2], ["03", 3], ["04", 4], ["05", 5], ["06", 6], ["07", 7], ["08", 8], ["09", 9], ["10", 10], ["11", 11], ["12", 12], ["13", 13], ["14", 14], ["15", 15], ["16", 16],
["17", 17], ["18", 18], ["19", 19], ["20", 20], ["21", 21], ["22", 22], ["23", 23], ["24", 24], ["25", 25], ["26", 26], ["27", 27], ["28", 28], ["29", 29], ["30", 30], ["31", 31]
START_YEAR = Time.now.year - 111
END_YEAR = Time.now.year
YEAR_RANGE = START_YEAR..END_YEAR
#Form Validation
validates :first_name, :presence => true,
:length => { :minimum => 2, :maximum => 15 },
:format => {
:with => alpha_regex,
:message => "Your first name must contain letters only"
}
validates :last_name, :presence => true,
:length => { :minimum => 2, :maximum => 15 },
:format => {
:with => alpha_regex,
:message => "Your last name must contain letters only"
}
validates :gender, :presence => true,
:inclusion => {
:in => %w( m f ), :message => "Are you male or female?"
}
before_save :prepare_birthday
validate :validate_birthday # if :birthday == nil
private
def prepare_birthday
begin
unless year.blank? # in order to avoid Year like 0000
self.birthday = Date.new(self.year.to_i, self.month.to_i, self.day.to_i)
end
rescue ArgumentError
false
end
end
def validate_birthday
errors.add(:birthday, "Birthday is invalid") unless prepare_birthday
end
end
Update method from controller
def update
respond_to do |format|
if @profile.update_attributes(params[:profile])
format.js { render :js => "window.location = '#{settings_edit_profile_path}'" }
flash[:success] = "Profile updated"
else
format.js { render :form_errors }
end
end
end
You shouldnt do it that way thats not the way it should be. In rails you should use the
date_selectform helper. => http://api.rubyonrails.org/classes/ActionView/Helpers/DateHelper.html#method-i-date_select When you use this helper it will automaticly work in creation and updating your objects!Another possibility would be to use the jQuerUI datepicker which is great too.