Rails Todo List app>任务在最后打印所有表记录

时间:2015-09-09 13:58:02

标签: ruby-on-rails ruby ruby-on-rails-4

我正在处理简单的Todo List应用程序,我想打印此输出(所有todo_task表记录 - > []括号" pranie"任务):

Title: a

Description: b

hey

albo

pranie
[#<TodoTask id: 1, todo_list_id: 1, content: "hey", created_at: "2015-09-09 12:46:11", updated_at: "2015-09-09 12:46:11">, #<TodoTask id: 2, todo_list_id: 1, content: "albo", created_at: "2015-09-09 12:48:42", updated_at: "2015-09-09 12:48:42">, #<TodoTask id: 3, todo_list_id: 1, content: "pranie", created_at: "2015-09-09 12:57:15", updated_at: "2015-09-09 12:57:15">] 

其他文件内容: app / controllers / todo_tasks_controller.rb:

class TodoTasksController < ApplicationController

  before_action :set_todo_list
  before_action :set_todo_task, except: [:create, :index]

  def create
    @todo_task = @todo_list.todo_tasks.create(todo_task_params)
    redirect_to @todo_list
  end

  def destroy
    @todo_task.destroy
    redirect_to @todo_list
  end

  def index
  end

  private

  def set_todo_list
    @todo_list = TodoList.find(params[:todo_list_id])
  end

  def set_todo_task
    @todo_task = @todo_list.todo_tasks.find(params[:id])
  end

  def todo_task_params
    params.require(:todo_task).permit(:content)
  end
end

app / views / todo_lists / index.html.erb:

<p>
  <strong>Title:</strong>
  <%= @todo_list.title %>
</p>

<p>
  <strong>Description:</strong>
  <%= @todo_list.description %>
</p>

<%= @todo_list.todo_tasks.each do |task| %>
    <p><%= task.content %></p>
<% end %>

<%= render 'todo_tasks/form' %>

app / views / todo_lists / _form.html.erb:

<%= form_for([@todo_list, @todo_list.todo_tasks.build]) do |f| %>
  <p>
    <%= f.text_field :content, placeholder: 'New Todo' %>
  </p>
  <p><%= f.submit %></p>
<% end %>

1 个答案:

答案 0 :(得分:2)

更改此

<%= @todo_list.todo_tasks.each do |task| %>
    <p><%= task.content %></p>
<% end %>

<% @todo_list.todo_tasks.each do |task| %>
    <p><%= task.content %></p>
<% end %>

请注意将<%=更改为<%

<% %> - 执行语句/表达式。

<%= %> - 打印输出。