Ruby on rails 简单表单日期输入不作为选择

Ruby on rails 简单表单日期输入不作为选择,ruby-on-rails,simple-form,Ruby On Rails,Simple Form,我使用简单的表单,我需要选择日期作为3个独立的输入字段(日-月-年) 这是我所拥有的,但绝对不是我想要的 <%= f.date_select :date_of_birth, start_year: Date.today.year - 16, end_year: Date.today.year - 100, order: [:day, :month, :year], label:

我使用简单的表单,我需要选择日期作为3个独立的输入字段(日-月-年)

这是我所拥有的,但绝对不是我想要的

<%= f.date_select :date_of_birth, start_year: Date.today.year - 16,
                            end_year: Date.today.year - 100,
                            order: [:day, :month, :year], label: false %>

谢谢你的帮助

到目前为止,我假设我必须用DB中的3个字段替换我的
出生日期
,然后简单地编写一个方法,将这些字段组合成实际的出生日期,我认为这是一条丑陋的道路,希望有更好的路。

试试看

= date_select 'object_name', :date_of_birth
您应该用对象名称替换“对象名称”。例如,user类的“user”,等等


但请记住,这种方法可能会破坏您的标记,因为simple_表单会向表单字段添加自己的div和其他包装器。所以您也应该添加它们。

我决定只向数据库添加3个字段:

  t.integer :birth_day
  t.integer :birth_month
  t.integer :birth_year
编写一些验证:

  validates :birth_day, 
            numericality: { only_integer: true, message: "Please enter numbers only." }, 
            length: { is: 2, message: "Should be 2 digits." },
            inclusion: { in: 1..31, message: 'should be in range 1..31'}
  validates :birth_month, 
            numericality: { only_integer: true, message: "Please enter numbers only." }, 
            length: { is: 2, message: "Should be 2 digits." },
            inclusion: { in: 1..12, message: 'should be in range 1..31'}
  validates :birth_year, 
            numericality: { only_integer: true, message: "Please enter numbers only." }, 
            length: { is: 4, message: "Should be 4 digits." },
            inclusion: { in: 1900..1996, message: 'should be in range 1900..1996'}
并将方法写入组装日期对象的方法:

  def date_of_birth
    "#{birth_day}/#{Date::MONTHNAMES[birth_month]}/#{birth_year}".to_date
  end

谢谢回答!这非常紧急,所以我坚持使用DB和assembly方法中的3个字段。