ActiveSupport :: TimeWithZone与12的比较失败

时间:2015-07-09 22:17:17

标签: ruby-on-rails ruby heroku

我有餐厅,用户可以预订。对于预订,我将时间属性设置为integer。然后,我使用帮助器显示时间选择(单选按钮),例如14到2:00 pm。

  def new_time(h)
    if h > 12
      (h-12).to_s + ":00 PM"
    elsif h == 12
      h.to_s + ":00 PM"
    else
      h.to_s + ":00 AM"
    end
  end

预订时间选项显示在餐厅页面上:

<%= form_for([@restaurant, @reservation], :html => {:class => 'reserve-form'}) do |f| %>
  <div class="times">
    <% @restaurant.hours_open.each do |h| %>
      <%= f.radio_button :time, h %>
      <%= f.label "time_#{h}", new_time(h), class: "reserve" %>
    <% end %>
  </div>
  <div class="align">
  <%= f.label :party_size,'Please enter party size' %>
  <%= f.text_field :party_size %>
  </div>
  <div class="align">
  <%= f.submit %>
  </div>
  

现在预订完成后,我会重定向到用户个人资料以显示该预订。在开发方面一切正常,但在部署到heroku后,我收到错误&#34;我们很抱歉,但出了点问题。&#34;在预订时,我认为它与下面显示的用户页面上显示的预订时间有关。

当我检查heroku日志时,这是错误:

ActionView::Template::Error (comparison of ActiveSupport::TimeWithZone with 12 failed):

        <% @user.reservations.each do |reservation| %>
        <div class="user-reservation align">
        <p>Restaurant: <%= reservation.restaurant.name %></p>
        <p>Reserved Time: <%= new_time(reservation.time) %></p>
        <p>Party Size: <%= reservation.party_size %></p>
        <p>Added on: <%= reservation.created_at.strftime("%B %d, %Y") %></p>
    app/helpers/application_helper.rb:3:in `>'
    app/helpers/application_helper.rb:3:in `new_time'
    app/views/users/show.html.erb:19:in `block in _app_views_users_show_html_erb__1649677434053595894_70057403913200'
    app/views/users/show.html.erb:16:in `_app_views_users_show_html_erb__1649677434053595894_70057403913200'

1 个答案:

答案 0 :(得分:0)

您正在将属性reservation.timeActiveSupport::TimeWithZone进行比较。

如果您的数据类型是时间,为什么不使用strftime格式化?

<p>Reserved Time: <%= reservation.time.strftime("%I:%M%p") %></p> 
相关问题