【问题标题】:Time dropdown for best_in_placebest_in_place 的时间下拉列表
【发布时间】:2013-05-05 19:43:58
【问题描述】:
我正在尝试用我的代码为 best_in_place gem 添加正确的语法:
<%= Time.at(task.hour).strftime("%I:%M %p") %>
我尝试过类似的东西
<%= best_in_place Time, :at(task.hour), :strftime("%I:%M %p") %>
但这似乎不起作用。有这方面的指点吗?
【问题讨论】:
标签:
ruby-on-rails
time
selector
best-in-place
【解决方案1】:
您可以使用 BIP 的 display_with 属性,并在您的 config/environments/xxx.rb 文件中添加自定义时间格式方法。此外,您应该使用实际对象而不是 Time 模型,在这种情况下它看起来是任务并且时间属性是小时?
在视图中:
<%= best_in_place task, :hour, :display_with => lambda { |t| t.to_s(:non_military_time) } %>
在 config/environments/development.rb、config/environments/production.rb、config/environments/test.rb 中(或者我确定您可以在一个地方声明它,但这就是我在过去):
Time::DATE_FORMATS[:non_military_time] = "%I:%M %p"
如果您不希望时间前导零,也可以在定义中使用%l 代替%I。希望这对某人有所帮助,即使这是一个老问题!