Ruby on Rails を使用して、7 日後に有効期限が切れたアイテムを自動的に削除する自動削除チェックリストを作成しています。しかし、アイテムはそのまま残り、マイナス (-1、-2、-3 など) になります。どこに問題があるのか正確にはわかりません。
私の破棄アクションは、他のすべてに対して機能しています。
Stack と Google を調べてみましたが、何も役に立ちません。
ここに私のアイテムコントローラがあります:
class ItemsController < ApplicationController
before_action :find_item, only: [:show, :edit, :update, :destroy]
def index
@items = Item.all
@items = Item.where(user: current_user)
end
def new
@item = Item.new
# authorize @item
end
def create
@item = current_user.items.build(item_params)
@item.expires_at = Time.now + 7.days
# authorize @item
if @item.save
redirect_to @item, notice: "Item was added!"
else
flash[:error] = "Error adding item. Please try again! Your organization depends on it!"
render :new
end
end
def show
@item = Item.find(params[:id])
@item.days_left
end
def edit
@item = Item.find(params[:id])
end
def update
@item = Item.find(params[:id])
if @item.update_attributes(item_params)
flash[:notice] = "Item was updated."
redirect_to item_path
else
flash[:error] = "There was an error saving the item. Please try again."
render 'edit'
end
end
def destroy
if @item.days_left == 0
@item.destroy
end
@item.destroy
redirect_to items_path
end
def completed
@item = Item.find(params[:id])
@item.update_attribute(:completed, true)
redirect_to items_path
end
private
def item_params
params.require(:item).permit(:name, :user, :description)
end
def find_item
@item = Item.find(params[:id])
end
end
そして Item モデル:
class Item < ActiveRecord::Base
belongs_to :user
def completed
completed == true
end
default_scope { order('expires_at ASC') }
def days_left
7 - (DateTime.now.to_date - created_at.to_date).to_i
end
end
Routes.rb
Rails.application.routes.draw do
get 'about' => 'welcome#about'
get "users/dashboard" => "items#index"
root to: 'welcome#index'
devise_for :users
resources :users, only: [:update, :show, :index]
resources :items do
member do
patch :completed
end
end
end
スキーマ.rb
ActiveRecord::Schema.define(version: 20150823054939) do
create_table "items", force: :cascade do |t|
t.string "name"
t.integer "user_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.boolean "completed"
t.datetime "expires_at"
t.text "description"
end
add_index "items", ["user_id"], name: "index_items_on_user_id"
create_table "users", force: :cascade do |t|
t.string "email", default: "", null: false
t.string "encrypted_password", default: "", null: false
t.string "reset_password_token"
t.datetime "reset_password_sent_at"
t.datetime "remember_created_at"
t.string "name"
t.integer "sign_in_count", default: 0, null: false
t.datetime "current_sign_in_at"
t.datetime "last_sign_in_at"
t.string "current_sign_in_ip"
t.string "last_sign_in_ip"
t.string "confirmation_token"
t.datetime "confirmed_at"
t.datetime "confirmation_sent_at"
t.string "unconfirmed_email"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "role"
end
add_index "users", ["email"], name: "index_users_on_email", unique: true
add_index "users", ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true
end