Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Ruby Build a Todo List Application with Rails 4 Build a Todo List Application with Rails 4 Adding Validations to Todo Items

I need help to figure out how to resolve this error.

1) Adding todo items is successful with valid content Failure/Error: click_link "New Todo Item" ActionView::Template::Error: First argument in form cannot contain nil or be empty # ./app/views/todo_items/new.html.erb:1:in _app_views_todo_items_new_html_erb__984293410__579391758' # ./spec/features/todo_items/create_spec.rb:16:inblock (2 levels) in <top (required)>'

5 Answers

Kevin Lozandier
Kevin Lozandier
Courses Plus Student 53,747 Points

Hi, Nicolas Nahimana

Review the code inside your controller to make sure you're defining and referring to the same variables inside your views.

Please provide your code wrapped around 3 grave characters before and after the code snippet to allow me and the rest of the Treehouse community be in a better position to help you more.

Hi Kevin, thanks for the assistance. I am going to review the code and I will inform you of my progress.

Gameli Ladzekpo
Gameli Ladzekpo
5,324 Points

Hey Kevin, I'm finding the same error

This is my 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_list = @todo_list.todo_items.new end

def create @todo_list = TodoList.find(params[:todo_list_id]) @todo_list = @todo_list.todo_items.new(todo_item_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


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

end (''')

This is my view

'''ruby <%= form_for [@todo_list, @todo_item] do |form| %> <%= form.label :content %> <%= form.text_field :content %>

<%= form.submit "Save" %>

<% end %>


This is my spec

'''ruby require 'spec_helper'

describe "Adding todo items" do let!(:todo_list) {TodoList.create(title:"Grocery List", description:"Groceries")}

def visit_todo_list(list)
    within "#todo_list_#{list.id}" do
        click_link "List Items"

it "is successful with valid content" do
    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")




Would really appreciate a point in the right direction.

Gameli Ladzekpo
Gameli Ladzekpo
5,324 Points

Okay - solved it my issue, Kevin you were right. i didn't actually declare a variable called @todo_item, i declared @todo_list twice by accident.