2014-09-25 51 views
0

我想在待辦事項列表應用程序中做任務,待辦事項將在7天后自毀。然而,就我現在的代碼而言,日期從創建待辦事項日期開始計算,而不是倒計時然後被銷燬。任何想法如何解決這個將不勝感激。耙子任務不能正常工作的自毀功能

這裏是我的待辦事項部分中的觀點:

<tr id="<%= dom_id(todo)%>"> 
    <td><%= todo.description %></td> 
    <td><%= distance_of_time_in_words_to_now(todo.created_at + 7.days) %></td> 
    <td><%= link_to "Completed", todo, method: :delete, remote: true, class: "btn btn-success" %></td> 
    </tr> 

這裏是我的rake任務代碼:

desc 'Removes items over a week old' 
task delete_items: :environment do 
    Todo.where("created_at <= ?", Time.now - 7.days).destroy_all 
end 

這裏是我的destroy.js.erb文件:

<% if @todo.destroyed? %> 
    $('#todo_' +<%= @todo.id %>).hide(); 
    $('.alert').html("<button type='button' class='close' data-dismiss='alert'>&times;</button><%= flash[:notice] %>"); 
    $('.alert').addClass('alert-success'); 
    $('.alert').removeClass('remove'); 
<% else %> 
    $('.alert').html("<button type='button' class='close' data-dismiss='alert'>&times;</button><%= flash[:error] %>"); 
    $('.alert').addClass('alert-danger'); 
    $('.alert').removeClass('remove'); 
<% end %> 

這裏是我的create.js.erb文件:

<% if @todo.valid? %> 
    $('.js-todos').prepend("<%= escape_javascript(render(@todo)) %>"); 
    $('.new-todo').html("<%= escape_javascript(render partial: 'todos/form') %>"); 
    $('.alert').html("<button type='button' class='close' data-dismiss='alert'>&times;</button><%= flash[:notice] %>"); 
    $('.alert').addClass('alert-success'); 
    $('.alert').removeClass('remove'); 
<% else %> 
    $('.alert').html("<button type='button' class='close' data-dismiss='alert'>&times;</button><%= flash[:error] %>"); 
    $('.alert').addClass('alert-danger'); 
    $('.alert').removeClass('remove'); 
    $('.new-todo').html("<%= escape_javascript(render partial: 'todos/form') %>") 
<% end %> 

如果您需要查看其他文件,請讓我知道。再次感謝!

編輯:添加的文件:

這裏是我的schedule.rb代碼:

set :output, "#{path}/log/cron.log" 

every 1.day do 
    rake "delete_items" 
end 

這裏是我的新deploy.rb文件:

require "bundler/capistrano" 

set :whenever_command, "bundle exec whenever" 
require "whenever/capistrano" 

# config valid only for Capistrano 3.1 
lock '3.2.1' 

set :application, 'my_app_name' 
set :repo_url, '[email protected]:me/my_repo.git' 

我需要把填寫set:application/set:repo_url以使deploy.rb文件正常工作?謝謝!

+1

你可以使用'7.days.ago'而不是'Time.now - 7.days'來使耙子任務更加可讀。 – pjmorse 2014-09-25 17:55:05

+0

耙子任務是否工作?也就是說,如果你運行'rake delete_items',它是否刪除了項目?這應該。是運行rake任務的代碼?我在這裏看到的只是模板和Javascript。 – pjmorse 2014-09-25 18:01:23

+0

最後,'distance_of_time_in_words_to_now(todo.created_at + 7.days)' - 你期望怎麼說?如果我剛創建了待辦事項,應該說「7天」。如果我明天回來,應該說「6天」。對? – pjmorse 2014-09-25 18:06:38

回答

0

您的表格將始終計數。忘記執行distance_of_time_in_words方法的複雜性,並考慮它的含義:如果您今天創建一個項目,那麼明天它的時間距離將爲1天。然後在後天的兩天。然後3天。爲創建時間添加7天將使其成爲-6天,然後-5天等(忽略如何格式化它)。當你點擊0時,待辦事項應該被刪除,並且不會顯示在桌子上,但是桌子只是顯示事物 - 它與一個項目是否被刪除沒有真正的關係;它只是通知用戶。刪除操作由您編寫的rake任務處理,當運行時,似乎正在運行

聽起來像這個問題是你的任務根本沒有運行。當您手動運行它時,根據您的報告,它可以正常工作,但您的調度程序無法定期運行。你有什麼計劃?調度程序是否在本地和生產環境中都不起作用?您使用哪些工具來安排時間,以及您如何使用它們?

因爲你的表格和調度程序是不同的問題,所以我會建議問一個關於rake調度失敗的新問題,在這個問題中你發佈了你的調度代碼(如果你使用的是什麼gems,你如何使用它們?在本地和生產中明確地(在調度器之外)運行任務的行爲是什麼?),並嘗試確定哪些部分調度rake任務失敗。

您的表格看起來基本正確。如果無法顯示所需的文字/數字,這只是distance_of_time_in_words方法的一個功能,以及您如何使用它。

include ActionView::Helpers::DateHelper # Module of which d_o_t_i_w_f_n is a method 
distance_of_time_in_words_from_now(5.days.ago) 
distance_of_time_in_words_from_now(24.hours.from_now) 

是要被刪除的項目都出現在表中出現,這可能有一切與調度和無關表的邏輯,所以在:與在Rails的控制檯或許試驗問題,我只專注於調度。