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

Dylan Cairns
Dylan Cairns
11,191 Points

expected: "Lots of Milk" got: "Milk"

I can't figure out why my edit and update functions aren't working. It doesn't change the content from Milk to Lots of Milk. Here's my spec

require 'spec_helper'

describe 'Editing todo items' do
    let!(:todo_list) { TodoList.create(title: "Grocery list", description: "Groceries")}
    let!(:todo_item) { todo_list.todo_items.create(content: "Milk") }
    #create todo list items for a single test before the visit 
    def visit_todo_list(list)
        visit "/todo_lists"
        within "#todo_list_#{todo_list.id}" do
            click_link "List items"
        end
    end 

    it "is successful with valid content" do
        visit_todo_list(todo_list)
        within("#todo_item_#{todo_item.id}") do
            click_link "Edit"
        end     
        fill_in "Content", with: "Lots of Milk"
        click_button "Save"
        expect(page).to have_content("Saved todo list item.")
        todo_list.reload
        expect(todo_item.content).to eq("Lots of Milk") 
    end
end

and my edit.html.erb

<%= form_for [@todo_list, @todo_item] do |form| %>
    div id="error_explanation">
        <h2><%= pluralize(@todo_item.errors.count, "error") %> prohibited this todo item from being saved: </h2>
        <!-- since todo_item is nested inside todo_list in the 
        routes file you must use both here in th form_for to get
        to it --> 
        <ul>
        <% @todo_item.errors.full_messages.each do |msg| %>
            <li> <%= msg %></li>
        <% end %> 
        </ul>
    </div> 
    <%= form.label :content %>
    <%= form.text_field :content %>

    <%= form.submit "Save" %>
<% end %> 
Maciej Czuchnowski
Maciej Czuchnowski
36,441 Points

Not enough information. We need at least your controller code, and preferably the whole project on github.

I'm having the same problem also!

1 Answer

Maciej Czuchnowski
Maciej Czuchnowski
36,441 Points

My first guess: you're reloading todo_list, not todo_item. Try doing:

todo_item.reload

instead of

todo_list.reload

Yup, you were right! Found that right after I posted last night.