I’m sure this is a stupid question, but I’ve got a todo list app I’ve built in Rails. There are projects and projects have many tasks. Aside from seeing projects with their tasks, I also wanted to see a simple listing of all of the tasks. The index action in the tasks controller looks like this:
def index
@tasks = Task.all
respond_to do |format|
format.html # index.html.erb
format.xml { render :xml => @tasks }
end
end
And in views/tasks/index.html.erb I simply had this to start:
<%= @tasks.each do |t| %>
<%= t.title %>,
<% end %>
But when I look at /tasks, I get this:
Task 1, Task 2, Task 3
#<Task:0x103225138>#<Task:0x1031ea998>#<Task:0x1031ea858>
I can’t figure out why the
#<Task:0x103225138>#<Task:0x1031ea998>#<Task:0x1031ea858>"
…are appearing or how to keep them from appearing. They appear even if I’m not printing anything other than the loop code. Any ideas? Thanks!
Will fix that. The problem is that you are printing with
=the result of.eachwhich returns the array it was called on, in this case@tasks. So your code is effectively doing this:A side note:
A much nicer way to do this is simply