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 Adding Validations to Todo Items

"Information" below todo item

Below my todo item, I see the following line

[#<TodoItem id: 1, todo_list_id: 2, content: "New item", string: nil, created_at: "2015-06-08 22:05:41", updated_at: "2015-06-08 22:05:41">]

I don't really know why that appears there... What I am doing wrong?

Edited: I have to learn Markdown...

3 Answers

Jeff Lange
Jeff Lange
8,788 Points

I think this is your issue here:

<%= @todo_list.todo_items.each do |todo_item| %>

See how you have the

with the "=" there? That tells erb to evaluate the statement and show it. Try it without the "=" as ```<%``` and see if that fixes it.  Without the equals sign tells erb to run the code, but not to show it.

Jeff,

Thank you very much for that! I can promise that I have looked at the code literally hundreds of times and totally missed that...

Thanks!

Jeff Lange
Jeff Lange
8,788 Points

You're welcome! It always helps to have an extra set of eyes. I do the same thing--It's incredible the stuff we look over when it's our own code. :)

Jeff Lange
Jeff Lange
8,788 Points

You'll need to post the relevant code. :)

Hi, thanks for your interest, I am really unable to find the error...

todo items index.html.er shows as follows

<h1><%= @todo_list.title %></h1>
<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_item) %>
            <%= link_to "Delete", todo_list_todo_item_path(todo_item), method: :delete, data: { confirm: "Are you sure?" } %>
        </li>
    <% end %>
</ul>
<p>
    <%= link_to "New Todo Item", new_todo_list_todo_item_path %>
</p>

Todo items controller as follows...

class TodoItemsController < ApplicationController
  before_action :find_todo_list
  def index
  end
  def new
    @todo_item = @todo_list.todo_items.new
  end
    def create
        @todo_item = @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
    end
  def edit
    @todo_item = @todo_list.todo_items.find(params[:id])
  end
  def update
    @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 destroy
    @todo_item = @todo_list.todo_items.find(params[:id])
    if @todo_item.destroy
      flash[:success] = "Todo list item was deleted."
    else
      flash[:error] = "Todo list item could not be deleted"
    end
    redirect_to todo_list_todo_items_path
  end
  def url_options
    { todo_list_id: params[:todo_list_id]}.merge(super)
  end
    private
  def find_todo_list
    @todo_list = TodoList.find(params[:todo_list_id])
  end
    def todo_item_params
        params[:todo_item].permit(:content)
    end
end

And todo list controller like this:

class TodoItemsController < ApplicationController
  before_action :find_todo_list
  def index
  end
  def new
    @todo_item = @todo_list.todo_items.new
  end
    def create
        @todo_item = @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
    end
  def edit
    @todo_item = @todo_list.todo_items.find(params[:id])
  end
  def update
    @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 destroy
    @todo_item = @todo_list.todo_items.find(params[:id])
    if @todo_item.destroy
      flash[:success] = "Todo list item was deleted."
    else
      flash[:error] = "Todo list item could not be deleted"
    end
    redirect_to todo_list_todo_items_path
  end
  def url_options
    { todo_list_id: params[:todo_list_id]}.merge(super)
  end
    private
  def find_todo_list
    @todo_list = TodoList.find(params[:todo_list_id])
  end
    def todo_item_params
        params[:todo_item].permit(:content)
    end
end

And I see that in the items list view:

Todo List

Item 1 Edit Delete
Item 2 - Modified Edit Delete

[#<TodoItem id: 10, todo_list_id: 4, content: "Item 1", string: nil, created_at: "2015-06-14 18:32:44", updated_at: "2015-06-14 18:32:44">, #<TodoItem id: 11, todo_list_id: 4, content: "Item 2 - Modified", string: nil, created_at: "2015-06-14 18:32:49", updated_at: "2015-06-14 18:32:55">]

I don't know why the text inside the "[]" appears. In fact, if there is no items in the list, just "[]" shows on the page...