尝试修改“Todo应用”

时间:2016-05-17 04:33:30

标签: ruby-on-rails ruby

我开始使用ruby on rails,我想尝试的第一件事就是修改“Todo app”示例。 我通过教程执行此操作:https://www.youtube.com/watch?v=fd1Vn-Wvy2w

在我完成后,我看到当我点击todo_list时,它会重定向到“show”表单todo_list,但现在我想要“show “在index上显示所有todo_list。我试着写

  <%= todo_items.content %> 
<{1>}页面上的

但是出现了一些错误。有没有解决方案,或者我应该在Controller页面上修改一些内容

index

应该能够显示在todo_items.content

1 个答案:

答案 0 :(得分:2)

您需要先在控制器操作中加载这些项目:

def index
  @todo_items = TodoItem.all
end

然后在index.html或您为此操作呈现的任何模板中,您可以呈现此集合:

<%= render @todo_items %>

应根据位于/app/views/todo_items/_todo_item.html.erb的视频呈现您应创建的todo_item部分内容。或者你可以这样做:

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

在控制器中:

def index
  @todo_lists = TodoList.all
end

在视图中:

<% @todo_lists.each_with_index do |list, index| %>
  List <%= index + 1 %> todos:
  <%= render list.todo_items %>
<% end %>