如何制作一个下拉菜单,反映数据库中相应列中存储的内容?
我有一个生日选择的下拉菜单,它可以更新数据库 但是在刷新的选择菜单中返回默认选项,因为我的所有文本字段都正在提取数据库数据。
我的表格:
<%= 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 %>
有什么线索我错过了什么? 亲切的问候
这是我的模特:
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
从控制器更新方法
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
答案 0 :(得分:1)
你不应该那样做那不是应该的方式。在rails中,您应该使用date_select
表单助手。 =&GT; http://api.rubyonrails.org/classes/ActionView/Helpers/DateHelper.html#method-i-date_select当您使用此帮助程序时,它将自动用于创建和更新对象!
另一种可能性是使用jQuerUI datepicker,这也很棒。