How can I modify the input type of the Rails datetime_select helper?

非 Y 不嫁゛ 提交于 2019-12-03 05:09:17

how about rolling your own simple helper, like

def datetime_text_fields(object_name, method)
  html = text_field_tag("#{object_name}[#{method}(3i)]", Date.today.day.to_s, :length => 2)
  html << select_month(Date.today, :field_name => "#{object_name}[#{method}(2i)]")
  html << text_field_tag("#{object_name}[#{method}(1i)]", Date.today.year.to_s, :length => 4)
  html << " "
  html << text_field_tag("#{object_name}[#{method}(4i)]", Time.now.hour.to_s, :length => 2)
  html << ":"
  html << text_field_tag("#{object_name}[#{method}(5i)]", Time.now.min.to_s, :length => 2)
end

Feel free to add more formatting stuff/separators etc. but it basically returns to correct field names for rails to be identified as DateTime. Rails expects fields named like date(1i), 1i = year, 2i = month etc.

Honestly I didn't test it or anything, but the output in console looked pretty convincing ;)

Why not roll your own, just name the input fields in the same exact way that the date picker helper does, and you get all the back-end benefits while rolling a custom front-end. Or use the :order to have your date picker give the month selection, and use some javascript magic for the other input.

You can use a jquery datepicker and populate hidden fields with the same naming convention as the rails datepicker.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!