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 trialAya Yousef
9,658 Pointsundefined method `content'
at the last stage of the course when I ran a test and I got this error in rspec
Viewing the todo items Displays the title of todo list displays no items when todo list is empty displays item content when a todo list has items (FAILED - 1)
Failures:
1) Viewing the todo items displays item content when a todo list has items
Failure/Error: click_link "List Items"
ActionView::Template::Error:
undefined method content' for nil:NilClass
# ./app/views/todo_items/index.html.erb:6:in
block in app_views_todo_items_index_html_erb3713388154754784548_70050973841440'
# ./app/views/todo_items/index.html.erb:5:in `_app_views_todo_items_index_html_erb_3713388154754784548_70050973841440'
# ./spec/features/todo_items/index_spec.rb:10:in block in visit_todo_list'
# ./spec/features/todo_items/index_spec.rb:9:in
visit_todo_list'
# ./spec/features/todo_items/index_spec.rb:31:in `block (2 levels) in <top (required)>'
Finished in 0.18019 seconds 3 examples, 1 failure
Failed examples:
rspec ./spec/features/todo_items/index_spec.rb:27 # Viewing the todo items displays item content when a todo list has items
Randomized with seed 30861
Here's my code:
in index.html.rb:
<h1><%= @todo_list.title %></h1> <p>Find me in app/views/todo_items/index.html.erb</p>
<ul class="todo_items"> <%= @todo_list.todo_items.each do |todo_item| %> <li><%= @todo_item.content %></li> <% end %> </ul>
in index_spec.rb:
require 'spec_helper'
describe "Viewing the todo items" do
let!(:todo_list){todo_list = TodoList.create(title: "al bayt", description: "al bayt goodies")}
def visit_todo_list(list)
visit "/todo_lists"
within "#todo_list_#{list.id}" do
click_link "List Items"
end
end
it "Displays the title of todo list" do
visit_todo_list(todo_list)
within "h1" do
expect(page).to have_content(todo_list.title)
end
end
it "displays no items when todo list is empty" do
visit_todo_list(todo_list)
expect(page.all("ul.todo_list li").size).to eq(0)
end
it "displays item content when a todo list has items" do
todo_list.todo_items.create(content: "Drinks")
todo_list.todo_items.create(content: "Food")
visit_todo_list(todo_list)
expect(page.all("ul.todo_items li").size).to eq(2)
within "ul.todo_items" do
expect(page).to have_content("Drinks")
expect(page).to have_content("Food")
end
end
end
2 Answers
Seth Reece
32,867 PointsHi Aya,
In todo_items/index.html.erb, the start of your each loop is written
<%= @todo_list.todo_items.each do |todo_item| %>
The
<%=
erb tag says to write the contents as html. You need to remove the = sign from that line so that it gets evaluated as ruby code.
Aya Yousef
9,658 PointsThank you Seth it worked now
Aya Yousef
9,658 PointsAya Yousef
9,658 PointsThank you Seth, I did as you said but I still get the same error
''' <h1><%= @todo_list.title %></h1> <p>Find me in app/views/todo_items/index.html.erb</p>
<ul class="todo_items"> <% @todo_list.todo_items.each do |todo_item| %> <li><%= @todo_item.content %></li> <% end %> </ul> '''
Seth Reece
32,867 PointsSeth Reece
32,867 PointsAhh, took me a bit to duplicate you error. In your loops you are creating a local variable called todo_item that you assign each @todo_list.todo_items to, but in your list item you using an instance variable @todo_items that doesn't exist. Therefore it is returning a value of nil. Change that to just todo_item.content and you should be good. P.S. When using markdown, Use the 3 `s then hit enter to drop down a line, then paste your code. Hit enter again (new line) for closing tag.