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

Roy Huang
Roy Huang
4,367 Points

First argument in form cannot contain nil or be empty

Hello I am doing Rail Development track and now I am on building a todo list. I came across a problem that I couldn't solve.

I am now in editing todo item part.

Firstly, I had

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

However, it doesn't work for me. Therefore, I still have to add@todo_list when I would like to make a link for view, as below:

<ul class="todo_items">
        <% @todo_list.todo_items.each do |todo_item| %>
        <li id="<%= dom_id(todo_item) %>">
            <%= todo_item.content %>
            <%= link_to "Edit", edit_todo_list_todo_item_path(@todo_list, todo_item) %>
        </li>
        <% end %>

Why does this happen?

======

However, the problem can be solved. I now came across another issue which I am not able to resolve.

I had my edit.html.erb:

<%= form_for [@todo_list, @todo_item] do |form| %>

  <% if @todo_item.errors.any? %>
    <div id="error_explanation">
      <h2><%= pluralize(@todo_item.errors.count, "error") %> prohibited this todo item from being saved:</h2>

      <ul>
      <% @todo_item.errors.full_messages.each do |message| %>
        <li><%= message %></li>
      <% end %>
      </ul>
    </div>
  <% end %>


    <%= form.label :content %>
    <%= form.text_field :content %>

    <%= form.submit "Save" %>
<%end%>

and defined edit in my controller

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

However, it just kept telling me that <font color="red">"ActionView::Template::Error: First argument in form cannot contain nil or be empty"

I have been doing researched for couple hours but couldn't find anything wrong. Please help me, thank you !

1 Answer

Roy Huang
Roy Huang
4,367 Points

Hello guys, I am a moron and I figure out what happened.

I just put

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

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

in my create method, no wonder it doesn't work....

Thank you guys. I still feel thrilled when I found the mistake.