Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/53.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 如何使下拉菜单反映';存储在其中';数据库中对应的列是什么?_Ruby On Rails_Ruby_Ruby On Rails 3_Rubygems_Ruby On Rails 3.1 - Fatal编程技术网

Ruby on rails 如何使下拉菜单反映';存储在其中';数据库中对应的列是什么?

Ruby on rails 如何使下拉菜单反映';存储在其中';数据库中对应的列是什么?,ruby-on-rails,ruby,ruby-on-rails-3,rubygems,ruby-on-rails-3.1,Ruby On Rails,Ruby,Ruby On Rails 3,Rubygems,Ruby On Rails 3.1,如何使下拉菜单反映数据库中相应列中存储的内容 我有一个生日选择下拉菜单,它可以很好地更新数据库 但在刷新时返回到“选择”菜单中的默认选项,因为所有“我的文本”字段都可以很好地提取db数据 我的表格: <%= form_for @profile, :remote => true, do |f| %> Username: <%= @profile.user.username %><br /> URL: http://site.com/<%=

如何使下拉菜单反映数据库中相应列中存储的内容

我有一个生日选择下拉菜单,它可以很好地更新数据库 但在刷新时返回到“选择”菜单中的默认选项,因为所有“我的文本”字段都可以很好地提取db数据

我的表格:

    <%= 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 %>

你不应该那样做这不是应该的。在rails中,您应该使用
date\u选择
form helper.=>当您使用此帮助器时,它将自动创建和更新您的对象


另一种可能是使用jQuerUI日期选择器,它也很好。

我两种都用过。您选择的日期有错误。如果我选择了一个错误的日期,它会表现出奇怪的行为,并放置s-1,-2等,并更改下拉菜单值,这就是为什么我这样做的原因。最后使用了date\u select
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