Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Ruby Build a Todo List Application with Rails 4 Build a Todo List Application with Rails 4 Deleting Todo Items

janeporter
PLUS
janeporter
Courses Plus Student 23,471 Points

i click on the delete button and get a 'template is missing...' error

In my Odot application, when I click on the Delete button for one of the grocery items, this is the error I get:

Template is missing
Missing template todo_items/show, application/show with {:locale=>[:en], :formats=>[:html], :variants=>[], :handlers=>[:erb, :builder, :raw, :ruby, :coffee, :jbuilder]}. Searched in: * "C:/Users/jporter/Documents/ruby_projects_tth/odot/app/views"

Rails.root: C:/Users/jporter/Documents/ruby_projects_tth/odot

Application Trace | Framework Trace | Full Trace
actionview (4.2.5) lib/action_view/path_set.rb:46:in `find'
actionview (4.2.5) lib/action_view/lookup_context.rb:121:in `find'
C:in `find_template'
actionview (4.2.5) lib/action_view/renderer/template_renderer.rb:40:in `determine_template'
actionview (4.2.5) lib/action_view/renderer/template_renderer.rb:8:in `render'
actionview (4.2.5) lib/action_view/renderer/renderer.rb:42:in `render_template'
actionview (4.2.5) lib/action_view/renderer/renderer.rb:23:in `render'
actionview (4.2.5) lib/action_view/rendering.rb:100:in `_render_template'
actionpack (4.2.5) lib/action_controller/metal/streaming.rb:217:in `_render_template'
actionview (4.2.5) lib/action_view/rendering.rb:83:in `render_to_body'
actionpack (4.2.5) lib/action_controller/metal/rendering.rb:32:in `render_to_body'
actionpack (4.2.5) lib/action_controller/metal/renderers.rb:37:in `render_to_body'
actionpack (4.2.5) lib/abstract_controller/rendering.rb:25:in `render'
actionpack (4.2.5) lib/action_controller/metal/rendering.rb:16:in `render'

what can i do to fix this?

janeporter
janeporter
Courses Plus Student 23,471 Points

the first error i get is this:

Unknown action
The action 'show' could not be found for TodoItemsController

first of all why would it look for a show action when i clicked delete? and when i tried adding the 'show' action to the TodoItemsController, i get the above error...

Alan Matthews
Alan Matthews
10,161 Points

In your view, when you added the link_to for the destroy action, did you specify the the method to use after the path helper? Something like this?

<%= link_to 'Delete', todo_list_path(@todo_list), method: :delete, data: { confirm: "Are you sure?" }

I believe Rails would just look for the show action when presented with a link_to without specifying the destroy action.

9 Answers

rigzin norboo
rigzin norboo
11,059 Points

Hey, I come across with similar problem on stackoverflow. Check this out

janeporter
PLUS
janeporter
Courses Plus Student 23,471 Points

Here is my code in my odot/app/views/todo_items/index.html.erb

<h1><%= @todo_list.title %></h1>

<ul class="todo_items">
    <% @todo_list.todo_items.each do |todo_item| %>
    <li id="<%= dom_id(todo_item) %>">
        <%= todo_item.content %>
        <%= link_to "Edit", edit_todo_list_todo_item_path(todo_item) %>
        <%= link_to "Delete", todo_list_todo_item_path(todo_item), method: :delete, data: { confirm: "Are you sure?" } %>
    </li>
    <% end %>
</ul>

<p>
    <%= link_to "New Todo Item", new_todo_list_todo_item_path %>
</p>

the statement is there but for whatever reason it doesn't work.

a similar statement is in the odot/app/views/todo_lists/index.html.erb, and that doesn't work either:

<p id="notice"><%= notice %></p>

<h1>Listing Todo Lists</h1>

<table>
  <thead>
    <tr>
      <th>Title</th>
      <th>Description</th>
      <th colspan="3"></th>
    </tr>
  </thead>

  <tbody>
    <% @todo_lists.each do |todo_list| %>
      <tr id="<%= dom_id(todo_list) %>">
        <td><%= todo_list.title %></td>
        <td><%= todo_list.description %></td>
        <td>
          <%= link_to "List Items", todo_list_todo_items_path(todo_list) %>
          <%= link_to 'Show', todo_list %>
          <%= link_to 'Edit', edit_todo_list_path(todo_list) %>
          <%= link_to 'Destroy', todo_list, method: :delete, data: { confirm: 'Are you sure?' } %>
        </td>
      </tr>
    <% end %>
  </tbody>
</table>

<br>

<%= link_to 'New Todo list', new_todo_list_path %>

would the fact that i have rails 4.2.5 on my local machine (on Windows 10 Pro) make a difference in required code syntax?

Alan Matthews
Alan Matthews
10,161 Points

No, the syntax should be the same. Could you post your rake routes or routes.rb? Also, could you post your destroy method in the todoitems_controller?

janeporter
PLUS
janeporter
Courses Plus Student 23,471 Points

Here is the destroy method in my odot/app/controllers/todo_items_controller.rb:

  def destroy
    @todo_item = @todo_list.todo_items.find(params[:id])
    if @todo_item.destroy
      flash[:success] = "Todo list item was deleted."
    else
      flash[:error] = "Todo list item could not be deleted."
    end
    redirect_to todo_list_todo_items_path
  end

and here is odot/spec/routing/todo_lists_routing_spec.rb

require "spec_helper"

describe TodoListsController do
  describe "routing" do

    it "routes to #index" do
      get("/todo_lists").should route_to("todo_lists#index")
    end

    it "routes to #new" do
      get("/todo_lists/new").should route_to("todo_lists#new")
    end

    it "routes to #show" do
      get("/todo_lists/1").should route_to("todo_lists#show", :id => "1")
    end

    it "routes to #edit" do
      get("/todo_lists/1/edit").should route_to("todo_lists#edit", :id => "1")
    end

    it "routes to #create" do
      post("/todo_lists").should route_to("todo_lists#create")
    end

    it "routes to #update" do
      put("/todo_lists/1").should route_to("todo_lists#update", :id => "1")
    end

    it "routes to #destroy" do
      delete("/todo_lists/1").should route_to("todo_lists#destroy", :id => "1")
    end

  end
end

and here is the odot/config/routes.rb file:

Rails.application.routes.draw do

  resources :todo_lists do 
    resources :todo_items
  end
  root 'todo_lists#index'
  # The priority is based upon order of creation: first created -> highest priority.
  # See how all your routes lay out with "rake routes".

  # You can have the root of your site routed with "root"
  # root 'welcome#index'

  # Example of regular route:
  #   get 'products/:id' => 'catalog#view'

  # Example of named route that can be invoked with purchase_url(id: product.id)
  #   get 'products/:id/purchase' => 'catalog#purchase', as: :purchase

  # Example resource route (maps HTTP verbs to controller actions automatically):
  #   resources :products

  # Example resource route with options:
  #   resources :products do
  #     member do
  #       get 'short'
  #       post 'toggle'
  #     end
  #
  #     collection do
  #       get 'sold'
  #     end
  #   end

  # Example resource route with sub-resources:
  #   resources :products do
  #     resources :comments, :sales
  #     resource :seller
  #   end

  # Example resource route with more complex sub-resources:
  #   resources :products do
  #     resources :comments
  #     resources :sales do
  #       get 'recent', on: :collection
  #     end
  #   end

  # Example resource route with concerns:
  #   concern :toggleable do
  #     post 'toggle'
  #   end
  #   resources :posts, concerns: :toggleable
  #   resources :photos, concerns: :toggleable

  # Example resource route within a namespace:
  #   namespace :admin do
  #     # Directs /admin/products/* to Admin::ProductsController
  #     # (app/controllers/admin/products_controller.rb)
  #     resources :products
  #   end
end
Alan Matthews
Alan Matthews
10,161 Points

Everything looks right to me, but it's hard to troubleshoot things over a forum. I searched stack overflow and found several people having a problem with their application.js file and the error your getting. Take a look at the application.js and change the code to this if yours is different:

// This is a manifest file that'll be compiled into application.js, which will include all the files
// listed below.
//
// Any JavaScript/Coffee file within this directory, lib/assets/javascripts, vendor/assets/javascripts,
// or any plugin's vendor/assets/javascripts directory can be referenced here using a relative path.
//
// It's not advisable to add code directly here, but if you do, it'll appear at the bottom of the
// compiled file.
//
// Read Sprockets README (https://github.com/sstephenson/sprockets#sprockets-directives) for details
// about supported directives.
//
//= require jquery
//= require jquery_ujs
//= require turbolinks
//= require_tree .
rigzin norboo
rigzin norboo
11,059 Points

can you post rake routes?

janeporter
janeporter
Courses Plus Student 23,471 Points

here are the rake routes:

C:\Users\jporter\Documents\ruby_projects_tth\odot\bin>rake routes
(in C:/Users/jporter/Documents/ruby_projects_tth/odot)
                  Prefix Verb   URI Pattern                                             Controller#Action
    todo_list_todo_items GET    /todo_lists/:todo_list_id/todo_items(.:format)          todo_items#index
                         POST   /todo_lists/:todo_list_id/todo_items(.:format)          todo_items#create
 new_todo_list_todo_item GET    /todo_lists/:todo_list_id/todo_items/new(.:format)      todo_items#new
edit_todo_list_todo_item GET    /todo_lists/:todo_list_id/todo_items/:id/edit(.:format) todo_items#edit
     todo_list_todo_item GET    /todo_lists/:todo_list_id/todo_items/:id(.:format)      todo_items#show
                         PATCH  /todo_lists/:todo_list_id/todo_items/:id(.:format)      todo_items#update
                         PUT    /todo_lists/:todo_list_id/todo_items/:id(.:format)      todo_items#update
                         DELETE /todo_lists/:todo_list_id/todo_items/:id(.:format)      todo_items#destroy
              todo_lists GET    /todo_lists(.:format)                                   todo_lists#index
                         POST   /todo_lists(.:format)                                   todo_lists#create
           new_todo_list GET    /todo_lists/new(.:format)                               todo_lists#new
          edit_todo_list GET    /todo_lists/:id/edit(.:format)                          todo_lists#edit
               todo_list GET    /todo_lists/:id(.:format)                               todo_lists#show
                         PATCH  /todo_lists/:id(.:format)                               todo_lists#update
                         PUT    /todo_lists/:id(.:format)                               todo_lists#update
                         DELETE /todo_lists/:id(.:format)                               todo_lists#destroy
                    root GET    /                                                       todo_lists#index
janeporter
PLUS
janeporter
Courses Plus Student 23,471 Points

Alan, My application.js file is exactly the same as what you posted.

rigzin norboo
rigzin norboo
11,059 Points

What rails and ruby version you are using. Everything seems find to me. For some reason it is using 'GET' verb instead of 'DELETE' verb although you have specified delete method in link_to .

janeporter
janeporter
Courses Plus Student 23,471 Points

I am using rails 4.2.5 and ruby 2.1.7 on Windows 10 pro (on a dell xps 15 9550 laptop).

janeporter
PLUS
janeporter
Courses Plus Student 23,471 Points

i made a couple of changes per one of the posts in the stackoverflow link you provided (don't ask which one...i don't remember) and it seems to be working now (at least it's not aborting when i delete a todo item). thank you for your help.