-1

I'm starting on ruby on rails, first thing I want to try is modifying the "Todo app" example. I do this by the tutorial: https://www.youtube.com/watch?v=fd1Vn-Wvy2w

After I finished, I saw that when I clicked on a todo_list, it will redirect to "show" form todo_list, but now I want the "show" display on the index with all the todo_list. I have tried to write

  <%= todo_items.content %> 

on the index page but it got some errors. Is there any solution or should I modify something on the Controller page so that

todo_items.content

should be able to display on the Index page

Ccr
  • 686
  • 7
  • 25
vicnoob
  • 1,169
  • 13
  • 27

1 Answers1

2

You need to load those items in your controller action first:

def index
  @todo_items = TodoItem.all
end

then in index.html or whatever template you are rendering for this action you can render this collection:

<%= render @todo_items %>

that should render an todo_item partial that you should have created based on your video located at /app/views/todo_items/_todo_item.html.erb. Or you can do:

<% @todo_items.each do |item| %>
  <%= item.content %>
<% end %>

In controller:

def index
  @todo_lists = TodoList.all
end

in view:

<% @todo_lists.each_with_index do |list, index| %>
  List <%= index + 1 %> todos:
  <%= render list.todo_items %>
<% end %>
rmagnum2002
  • 11,341
  • 8
  • 49
  • 86
  • Thanks it seems it worked but this will show all the item of all todo_list under each Todo_list, what i want to do is only show items of a list below it – vicnoob May 17 '16 at 07:36
  • i want something like in the "Show" page of that example <%= render @todo_list.todo_items %> but the error now is "undefined method `todo_items' for nil:NilClass", i dont know what to put in the todo_list controller now – vicnoob May 17 '16 at 07:42
  • hmm it's not work, here is the picture https://drive.google.com/file/d/0B6QMY6BEMRcFMUZxMnBXZG13NHc/view?usp=sharing – vicnoob May 17 '16 at 16:10
  • That's because of parameters you are sending to your link, @todo_list is nil in your case, you should send todo_list as params to render method (http://stackoverflow.com/a/9827701/985241). If you'll comment the link in your partial it should display the items correctly or pass it as described in link above and use it in partial. Anyway, that's different from what it was required in your question. – rmagnum2002 May 17 '16 at 18:55