2015-07-09 52 views
0

我有餐館,用戶可以預訂。對於預訂,我將我的時間屬性設置爲integer。然後,我使用助手來顯示時間選擇(單選按鈕),例如14點到2點。ActiveSupport :: TimeWithZone與12次失敗的比較

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後,我得到錯誤「我們很抱歉,但出錯了。」當進行預訂時,我認爲它與預訂時間顯示在下面顯示的用戶頁面上有關。

當我檢查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' 

回答

0

你是比較屬性reservation.time這是一個ActiveSupport::TimeWithZone一個整數。

如果你的數據類型是一個時間,爲什麼不使用strftime來格式化它呢?

<p>Reserved Time: <%= reservation.time.strftime("%I:%M%p") %></p> 
+0

該數據類型不是時間,因爲我已將它設置爲整數。這是我的模式:t.integer「time」 – Ezde

+0

以及提供的代碼,我看不出任何問題......但爲什麼不使用時間數據類型? – jazzytomato

+0

好的會做..感謝 – Ezde