date_select和time_select没有在rails中获取start_year和end_year以及order参数?

时间:2015-09-23 12:55:48

标签: ruby-on-rails date time

我使用导轨date_selecttime_select以及以下选项。 但对于date_select start_yearend_year以及order选项不起作用。并且time_select ampm:true选项无效。

    <%= label_tag :date,"Date" %>
    <%= date_select :date, { start_year: Date.today.year - 1, :end_year => Date.today.year, order: [:day, :month, :year] }  %>

    <%= label_tag :check_in,"Check In Time" %>
    <%= time_select :check_in, {ampm: true} , {}  %>

    <%= label_tag :check_out,"Check Out Time" %>
    <%= time_select :check_out, {ampm: true}, {}   %>

以及如何应用类属性。我喜欢这样,但造型不起作用。

    <%= time_select :check_out, {ampm: true} ,{:class => 'form-control' }  %>

2 个答案:

答案 0 :(得分:2)

在Rails上API docs date_select定义为

  

date_select(object_name,method,options = {},html_options = {})

当你应该是第三个参数时,你将options作为第二个参数。

解决方案1:

<%= date_select :date, nil, { start_year: Date.today.year - 1, :end_year => Date.today.year, order: [:day, :month, :year] }  %>

time_select

存在同样的问题
  

time_select(object_name,method,options = {},html_options = {})

解决方案2:

 <%= time_select :check_out, nil, {ampm: true} ,{:class => 'form-control' }  %>

答案 1 :(得分:1)

参数顺序错误。它应该是:

<%= label_tag :date,"Date" %>
<%= date_select :date, {}, { start_year: Date.today.year - 1, :end_year => Date.today.year, order: [:day, :month, :year] }  %>

<%= label_tag :check_in,"Check In Time" %>
<%= time_select :check_in, {}, {ampm: true} %>

<%= label_tag :check_out,"Check Out Time" %>
<%= time_select :check_out, {}, {ampm: true}   %>

另请参阅:http://api.rubyonrails.org/classes/ActionView/Helpers/DateHelper.html#method-i-date_select