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 Adding Todo Items

click_link "List Items" error

Adding todo items is successful with valid content (FAILED - 1)

Failures:

1) Adding todo items is successful with valid content Failure/Error: click_link "List Items" ActionView::Template::Error: undefined local variable or method new_todo_list_todo_item' for #<#<Class:0x007fde1306c590>:0x007fde13067e00> # ./app/views/todo_items/index.html.erb:11:inapp_views_todo_items_index_html_erb_2144034651824161557_70295912349600' # ./spec/features/todo_items/create_spec.rb:9:in block in visit_todo_list' # ./spec/features/todo_items/create_spec.rb:8:invisit_todo_list' # ./spec/features/todo_items/create_spec.rb:14:in `block (2 levels) in <top (required)>'

can anyone help me with why I am getting this error and how to fix it?

create_spec.rb

require 'spec_helper'
describe "Adding todo items" do 
    let!(:todo_list) { TodoList.create(title: "Grocery list", description: "Groceries")}
    def visit_todo_list(list)
        visit "/todo_lists"
        within "#todo_list_#{list.id}" do 
            click_link "List Items"
        end
    end 
    it "is successful with valid content" do 
        visit_todo_list(todo_list)
        click_link "New Todo Item"
        fill_in "Content", with: "Milk"
        click_button "Save"
        expect(page).to have_content("Added todo list item.")
        within("ul.todo_items") do 
            expect(page).to have_content("Milk")
        end
    end
end

routes.rb

Rails.application.routes.draw do
  resources :todo_lists do 
    resources :todo_items
  end  
  root "todo_lists#index"
end

index.html.erb

<h1><%= @todo_list.title %></h1>
<ul class="todo_items">
    <% @todo_list.todo_items.each do |todo_item| %>
    <li><%= todo_item.content %></li>
    <% end %>
</ul>
<p>
    <%= link_to "New Todo Item", new_todo_list_todo_item %>
</p>

todo_items_controller.rb

class TodoItemsController < ApplicationController
  def index
    @todo_list = TodoList.find(params[:todo_list_id])
  end
  def new
    @todo_list = TodoList.find(params[:todo_list_id])
    @todo_item = @todo_list.todo_items.new
  end   
  def create
    @todo_list = TodoList.find(params[:todo_list_id])
    @todo_item = @todo_list.todo_items.new(todo_item_params)
    if @todo_item.save
        flash[:success] = "Added todo list item."
        redirect_to todo_list_todo_items_path
    else
        flash[:error] = "There was a problem adding that todo list item."
        render action: :new
    end     
  end   
  private 
  def todo_item_params
    params[:todo_item].permit(:content)
  end   
end

new.html.erb

<%= form_for [@todo_list, @todo_item] do |form| %>
    <%= f.label :content %>
    <%= f.text_field :content %>
    <%= f.submit "Save" %>
<% end %>

1 Answer

Seth Reece
Seth Reece
32,867 Points

Hi Jonathan,

In your index.html.erb you have a link to:

<%= link_to "New Todo Item", new_todo_list_todo_item %>

links should always be pointed at a path e.g.

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

Thank you Seth! You just fixed my problem.