Couldn't find Item with 'id'. Paper trail issue, unable to undo action

229 views Asked by At

My To-do list application, I want to be able to undo changes.

If the task(item) is completed or deleted, I have an undo hyperlink to revert the change. I am using the paper trail gem file. enter image description here

The URL is http://localhost:3000/versions/315/revert Looks like it is not reverting back and picking the item, to revert the change.

I have the versions controller with fi statements that if the Item is blank, return record not found. As it is not returning the message it must have something?

I don't know what I am doing wrong here, any help would be greatly appreciated.

Here is my main Task (item) controller.rb

 class ItemsController < ApplicationController
    before_action :find_item, only: [:show, :edit, :update, :destroy]

    def index
        if user_signed_in?
            @items = Item.where(:user_id => current_user.id).order("created_at DESC")
        end
    end

    def show
    end

    def new
        @item = current_user.items.build
    end

    def create
        @item = current_user.items.build(item_params)
        if @item.save
            redirect_to root_path, :notice =>  "Congrats! Task was created!"
        else
            render 'new' 
        end
    end

    def edit
    end

    def update
        if @item.update(item_params)
            redirect_to item_path(@item), :notice =>  "Congrats! Task was updated!"
        else
            render 'edit'
        end
    end

    def destroy
        @item.destroy
        redirect_to root_path , :notice => "Task was Deleted! To Revert click. #{undo_link}"
    end

    def complete
        @item = Item.find(params[:id])
        @item.update_attribute(:completed_at, Time.now)
        redirect_to root_path , :notice => "To undo completed task please click. #{undo_link}"
        
    end
    

    private
    
    def item_params
        params.require(:item).permit(:title, :description)
    end

    def find_item
        @item = Item.find(params[:id])
    end
    def undo_link
        view_context.link_to("undo",revert_version_path(@item.versions.scope.last), :method => :post)
    end
    
end

Here is the versions controller

    class VersionsController < ApplicationController
        PaperTrail::Version.where('created_at < ?', 1.day.ago).delete_all
        def revert
            @item = Item.find(params[:id])
            if @item.blank?
            format.html {redirect_to(rooth_path, :notice => 'Record not found') }
            elsif @item.reify
            @item.reify.save!
            else
            @item.item.destroy
            end
            link_name = params[:redo] == "true" ? "undo" : "redo"
            link = view_context.link_to(link_name, revert_version_path(@item.next, :redo => !params[:redo]), :method => :post)
            redirect_to :back, :notice => "Undid #{@item.event}. #{link}"
     end
    
end

Here is my Item model (tasks)

    class Item < ActiveRecord::Base
    belongs_to :user
    has_paper_trail  # you can pass various options here
    def completed?
        !completed_at.blank?
    end

end

Here is my routes folder

    Rails.application.routes.draw do
    post "versions/:id/revert" => "versions#revert", :as => "revert_version"
    devise_for :users
    resources :items do
        member do
            
            patch :complete
            
        end

    end
    root 'items#index'
        get '/about' => 'page#about'
        
end
0

There are 0 answers