【问题标题】:Rails date/year fieldRails 日期/年份字段
【发布时间】:2013-06-05 06:27:33
【问题描述】:

我只想在 Rails 表单中输入年份字段。我试过select_year,但它对我不起作用。 select_date 也给了我问题,因为我的表单模型是关联的。有没有替代的想法。

【问题讨论】:

  • select_yearselect_date 遇到了什么问题?
  • 您还想要年份下拉或输入字段吗?
  • f.select_year 出错
  • select_date 给了我三个字段的数据
  • 这些字段是什么?

标签: ruby-on-rails ruby-on-rails-3 datetime


【解决方案1】:

参考:-select_year

# Generates a select field for years that defaults to the current year that
# is named 'birth' rather than 'year'
<%= f.select_year(Date.today, :field_name => 'birth') %>

但如果您没有任何列,则只需使用

<%= select_year(Date.today) %>

参考this 并尝试类似

<%= select_year Date.today, :start_year => Time.now.year, :end_year => Time.now.year - 95, :field_name => :grad_year, :prefix => :profile  %>

【讨论】:

  • #<:formbuilder:0x007f8604248858> 的未定义方法 `select_year'
  • 然后使用&lt;%= select_year(Date.today, :field_name =&gt; 'birth') %&gt;
  • 这仅适用于表单,但在提交数据时给我带来了问题
  • 因为模型是关联的,并且接受_nested_attributes
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-03-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-09-11
相关资源
最近更新 更多