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 trialUnsubscribed User
11,042 PointsGetting "undefined method `todo_item'"
Tried to search in the forum, nothing solved... Here are my files:
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
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_item.new
end
end
routes.rb
Rails.application.routes.draw do
resources :todo_lists do
resources :todo_items
end
root 'todo_lists#index'
end
todo_items.rb
class TodoItem < ActiveRecord::Base
belongs_to :todo_list
end
todo_list.rb
class TodoList < ActiveRecord::Base
has_many :todo_items
validates :title, presence: true
validates :title, length: {minimum: 3}
validates :description, presence: true
validates :description, length: {minimum: 5}
end
when I try to run rspec on create_spec.rb i got this:
Adding todo items
Is successful with valid content (FAILED - 1)
Failures:
1) Adding todo items Is successful with valid content
Failure/Error: click_link "New Todo Item"
NoMethodError:
undefined method `todo_item' for #<TodoList:0x007fcdb49b0288>
# ./app/controllers/todo_items_controller.rb:8:in `new'
# ./spec/features/todo_items/create_spec.rb:17:in `block (2 levels) in <top (required)>'
Finished in 0.26657 seconds
1 example, 1 failure
Failed examples:
rspec ./spec/features/todo_items/create_spec.rb:15 # Adding todo items Is successful with valid content
Thanks for your time :-)
3 Answers
Unsubscribed User
11,042 PointsHoly cow... I feel really embarassed! :-) Thanks again Seth, you made my day!
johnathan smith
2,569 PointsI think you might need to require the todo items controller file in the head of your spec file. I believe it would be something like require_relative 'todo_items'
Unsubscribed User
11,042 Pointsit didn't solve it :(
Seth Reece
32,867 PointsHi Francesco,
Since your TodoList model has many todo_items, you want to create todo_items.new in your TodoItemsController. Once you have one or more todo_items, those can be called as a todo_item with an id. e.g.
class TodoItemsController < ApplicationController
def index
@todo_list = TodoList.find(params[:todo_list_id])
end
def new
# set the instance variable @todo_list to the current TodoList
@todo_list = TodoList.find(params[:todo_list_id])
# set the instance variable @todo_item equal to a new object in the todo_items table that belongs to the @todo_list
@todo_item = @todo_list.todo_items.new
end
end
Hopefully that makes sense.
Unsubscribed User
11,042 PointsHi Seth, are you meaning that I have to replace "def new" with "def todo_items.new"? thanks for your time :-)
Seth Reece
32,867 PointsNope, in the new method, change:
@todo_item = @todo_list.todo_item.new
to:
@todo_item = @todo_list.todo_items.new