【问题标题】:Build select date and year inputs with simple_form使用 simple_form 构建选择日期和年份输入
【发布时间】:2015-03-10 17:32:13
【问题描述】:

在我的 Ruby on Rails 应用程序中,我有一些表单需要输入来选择月份和年份。我使用内置的 Rails select_date 输入来做到这一点:

 = select_date @date, :order => [ :month, :year], :discard_day => true

但现在我想知道是否有任何方法可以让 simple_form 变得同样简单?

【问题讨论】:

    标签: ruby-on-rails ruby simple-form


    【解决方案1】:

    您好,请阅读此文档

    1.4 其他感兴趣的助手 其他值得一提的表单控件是文本区域、密码字段、隐藏字段、搜索字段、电话字段、日期字段、时间字段、颜色字段、日期时间字段、日期时间本地字段、月份字段、星期字段、URL 字段、电子邮件字段、数字字段和范围字段:

    <%= text_area_tag(:message, "Hi, nice site", size: "24x6") %>
    <%= password_field_tag(:password) %>
    <%= hidden_field_tag(:parent_id, "5") %>
    <%= search_field(:user, :name) %>
    <%= telephone_field(:user, :phone) %>
    <%= date_field(:user, :born_on) %>
    <%= datetime_field(:user, :meeting_time) %>
    <%= datetime_local_field(:user, :graduation_day) %>
    <%= month_field(:user, :birthday_month) %>
    <%= week_field(:user, :birthday_week) %>
    <%= url_field(:user, :homepage) %>
    <%= email_field(:user, :address) %>
    <%= color_field(:user, :favorite_color) %>
    <%= time_field(:task, :started_at) %>
    <%= number_field(:product, :price, in: 1.0..20.0, step: 0.5) %>
    <%= range_field(:product, :discount, in: 1..100) %>
    

    http://guides.rubyonrails.org/form_helpers.html

    这里还有更多示例:

    http://apidock.com/rails/ActionView/Helpers/DateHelper/date_select

    【讨论】:

    【解决方案2】:

    显然是一个老问题,但答案是肯定的,你绝对可以用 simple_form 做同样的事情。为寻找此答案的其他人发布。

    您将传递 date 作为输入类型,然后就像 select_date form_for 输入助手一样,您将传递 discard_day

    所以它可能看起来像这样:

    = f.input :your_date_field, as: :date,  discard_day: true
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多