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

undefined method completed_at

I don't get why, I've already looked at every line and I don't see the issue

complete_spec.rb

require 'spec_helper'

describe "Completing todo items" do
  let!(:todo_list) { TodoList.create(title: "Grocery list", description: "Groceries") }
  let!(:todo_item) { todo_list.todo_items.create(content: "Milk") }  

  it "is successful when marking a single item complete" do
    expect(todo_item.completed_at).to be_nil
    visit_todo_list todo_list
    within dom_id_for(todo_item) do
        click_link "Mark Complete"
    end
    todo_item.reload
    expect(todo_item.completed_at).to to_not be_nil
  end
end

Terminal error

F

Failures:

  1) Completing todo items is successful when marking a single item complete
     Failure/Error: expect(todo_item.completed_at).to be_nil
     NoMethodError:
       undefined method `completed_at' for #<TodoItem:0xbaaa5798>
     # ./spec/features/todo_items/complete_spec.rb:8:in `block (2 levels) in <top (required)>'

Finished in 0.17751 seconds
1 example, 1 failure

Failed examples:

rspec ./spec/features/todo_items/complete_spec.rb:7 # Completing todo items is successful when marking a single item complete

Randomized with seed 6624

Can you push the current version to github? The one published there does not have completed_spec file in it.

4 Answers

Your migration is there, but the completed_at was not applied to the schema. If you run rake db:migrate:status it will probably tell you that one of the migrations is not up. Just in case, do these two things to make sure that the migrations are also applied to test database:

rake db:migrate

and

rake db:migrate RAILS_ENV=test

Still same error

OK, I'll test it and let you know.

I don't get your error when running locally. Does your db/schema.rb have 'completed_at' attribute in the todo_item? Try doing rake db:reset and rake db:reset RAILS_ENV=test and then tell me what you get when you run rake db:migrate:status. You should then get a routing error in your test, because your Mark Complete link does not have the patch method in it, it should look like this:

<%= link_to "Mark Complete", complete_todo_list_todo_item_path(todo_item), method: :patch %>

It's working now, I can go along with the tutorial thanks Maciej!

Good to hear :)

Any idea why no matter what action I do it makes the alert the color of a notice. I thought success would be green, error red, and only notice blue. I only ever get a blue color alert.

In your controller you always render a notice. You'd have to play around if you want different colors and styles. This article could help you, experiment a bit and retract changes using git if something breaks ;)

https://agilewarrior.wordpress.com/2014/04/26/how-to-add-a-flash-message-to-your-rails-page/

I never tried doing these things, so I can't tell you exactly what to do at the moment.

i think its up now

Here's what I get:

treehouse:~/projects/odot (master *) $ bin/rake db:reset
-- create_table("todo_items", {:force=>true})
   -> 0.0409s
-- add_index("todo_items", ["todo_list_id"], {:name=>"index_todo_items_on_todo_list_id"})
   -> 0.0079s
-- create_table("todo_lists", {:force=>true})
   -> 0.0100s
-- initialize_schema_migrations_table()
   -> 0.0227s
treehouse:~/projects/odot (master *) $ bin/rake db:reset RAILS=ENV=test
-- create_table("todo_items", {:force=>true})
   -> 0.0523s
-- add_index("todo_items", ["todo_list_id"], {:name=>"index_todo_items_on_todo_list_id"})
   -> 0.0080s
-- create_table("todo_lists", {:force=>true})
   -> 0.0100s
-- initialize_schema_migrations_table()
   -> 0.0240s
treehouse:~/projects/odot (master *) $ bin/rake db:migrate:status

database: /home/treehouse/projects/odot/db/development.sqlite3

 Status   Migration ID    Migration Name
--------------------------------------------------
   up     20141113013217  Create todo lists
   up     20141114000255  Create todo items
   up     20141115143215  Add completed at to todo items

treehouse:~/projects/odot (master *) $ 

When I click on the "Mark Complete" I get this error:

NoMethodError in TodoItemsController#complete

undefined method `completed_at=' for #<TodoItem:0xba8d443c>

def complete
    @todo_item = @todo_list.todo_items.find(params[:id])
    @todo_item.update_attribute(:completed_at, Time.now)
    redirect_to todo_list_todo_items_path, notice: "Todo item was marked as complete."
  end

It highlights the 3rd row in red on the page.

Push your current code and I'll check it out.