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

Philip Bessa
Philip Bessa
5,396 Points

Undefined method 'todo_items_params'?

I have to believe they mistakenly placed this course in the beginner track. I'm just following along and learning next to nothing at this point because these videos just assume I know all there is to know about Ruby. Spending over three hours on a 9 minute video and I'm still stuck, so I give up for today.

Failures:

  1) Editing todo items is successful with valid content
     Failure/Error: click_button "Save"
     NameError:
       undefined local variable or method `todo_item_params' for #<TodoItemsController:0xb98d3088>
     # ./app/controllers/todo_items_controller.rb:31:in `update'
     # ./spec/features/todo_items/edit_spec.rb:20:in `block (2 levels) in <top (required)>'

Finished in 0.40751 seconds
1 example, 1 failure

Failed examples:

rspec ./spec/features/todo_items/edit_spec.rb:14 # Editing todo items is successful with valid content

Randomized with seed 17659

This error arrives just after the part where he copy/pastes code out of thin air after the 6:00 mark. I don't have code on github since it was never covered to push it to there, and I'd need to go back and redo that course to remember how to.

controller:

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_items_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

  def edit
    @todo_list = TodoList.find(params[:todo_list_id])
    @todo_item = @todo_list.todo_items.find(params[:id])
  end

  def update
    @todo_list = TodoList.find(params[:todo_list_id])
    @todo_item = @todo_list.todo_items.find(params[:id])
    if @todo_item.update_attributes(todo_item_params)
       flash[:success] = "Saved todo list item."
       redirect_to todo_list_todo_items_path
    else
       flash[:error] = "That todo item could not be saved."
       render action: :edit
    end
  end

  def url_options
    {todo_list_id: params[:todo_list_id]}.merge(super)
  end


  private
  def todo_items_params
    params[:todo_item].permit(:content)
  end
end

edit:

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")}

    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)
        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_item.reload
        expect(todo_item.title).to eq("Lots of Milk")
    end
end

Any other code I need to paste? I can't figure this out. I simply can't tell what's wrong due to the entire disconnect this course seems to have.

Is RSpec and TDD taught in a better format in another track by chance as well?

Philip Bessa
Philip Bessa
5,396 Points

Funny thing, I compared my code side-by-side to another by diverging my eyes (like for looking at Magic Eye books to see 3D objects) and matching my code, a single typo was found:

def todo_items_params

Nevertheless, I'm seriously doubting my ability to finish this course.

1 Answer

Hi Philip,

It looks like you have a typo 3 lines from the bottom of your TodoItemsController.

It should be

def todo_item_params

without the 's' in item(s)

Hope that helps! Tyler