我有一個「Log」項目,每個日誌都有:日期,:小時,:描述。我只是試圖確定一週內工作了多少小時,但是無法確定代碼的正確分離。讓我知道是否需要任何進一步的代碼。軌道3Ruby on Rails:本週工作的小時數
log.rb
def self.days_in_range(from, to)
Log.where(:date => (from.to_date)..(to.to_date))
end
index.html.erb
<% content_for :sidebar do %>
<h4> Sidebar Content </h4>
<ul>
<li>Hours worked this week:
<%= Log.hours_this_week %> # unsure how to call
</li>
<li>Hours worked in total:
<%= Log.sum(:hours) %>
</li>
<li>Most hours worked in a day:
<%= Log.maximum(:hours) %>
</li>
</ul>
<% end %>
logs_helper.rb?
def hours_this_week
today = Time.now
day_of_week = today.wday
sunday = today - day_of_week.days
days = Log.days_in_range(today, sunday)
hours = 0
days.each do |day|
hours += day.hours
end
end
[解決] 錯誤
Showing /Users/***/Documents/workspace/***/hours_tracker/hours/app/views/logs/index.html.erb where line #33 raised:
undefined method `hours_this_week' for #<LogsController:0x103b66be8>
Extracted source (around line #33):
30: <h4> Sidebar Content </h4>
31: <ul>
32: <li>Hours worked this week:
33: <%= hours_this_week %>
34: </li>
35: <li>Hours worked in total:
36: <%= Log.sum(:hours) %>
Rails.root: /Users/***/Documents/workspace/***/hours_tracker/hours
[更新] 新的錯誤
錯誤
ArgumentError in Logs#index
Showing /Users/***/Documents/workspace/***/hours_tracker/hours/app/views/logs/index.html.erb where line #33 raised:
wrong number of arguments (0 for 1)
Extracted source (around line #33):
30: <h4> Sidebar Content </h4>
31: <ul>
32: <li>Hours worked this week:
33: <%= hours_this_week %>
34: </li>
35: <li>Hours worked in total:
36: <%= Log.sum(:hours) %>
Rails.root: /Users/***/Documents/workspace/***/hours_tracker/hours
LogsHelper模塊內部的幫助器方法? – 2011-06-16 17:17:41
是的,但是我從我的觀點來看,這是否有效? – Adam 2011-06-16 17:29:01