我创建了三个模型,Task
Detail
和Record
。
在浏览器上显示我的观看次数时,与Detail
相关的所有内容都会消失,而与Task
和Record
相关的内容仍然正常。
这些是app / models中的模型
class Detail < ActiveRecord::Base
belongs_to :record
belongs_to :task
end
class Record < ActiveRecord::Base
has_many :details
end
class Task < ActiveRecord::Base
has_many :details
end
分贝/迁移
class CreateTasks < ActiveRecord::Migration
def change
create_table :tasks do |t|
t.string :title
t.string :cell
t.boolean :done
t.timestamps
end
end
end
class CreateDetails < ActiveRecord::Migration
def change
create_table :details do |t|
t.string :title
t.string :valve
t.string :cell
t.string :description
t.boolean :done
t.integer :order
t.references :task
t.references :record
t.timestamps
end
end
end
class CreateRecords < ActiveRecord::Migration
def change
create_table :records do |t|
t.string :cell
t.string :valve
t.string :state
t.timestamps
end
end
end
分贝/迁移/ seeds.rb
r1 = Record.create(cell: "cell1", valve: "A", state: "N/A")
r2 = Record.create(cell: "cell1", valve: "B", state: "N/A")
r3 = Record.create(cell: "cell2", valve: "C", state: "N/A")
r4 = Record.create(cell: "cell2", valve: "D", state: "N/A")
t1 = Task.create(title: "Task1", cell: "cell1", done: false)
Detail.create(title: "Detail1", description: "This is detail1.1", order: 1,
valve: "A", task_id: t1.id, done: false, cell: "cell1", record_id: r1.id)
Detail.create(title: "Detail2", description: "This is detail2.1", order: 2,
valve: "B", task_id: t1.id, done: false, cell: "cell1", record_id: r2.id)
t2 = Task.create(title: "Task2", cell: "cell2", done: false)
Detail.create(title: "Detail3", description: "This is detail3", order: 1,
valve: "C", task_id: t2.id, done: false, cell: "cell2", record_id: r3.id)
Detail.create(title: "Detail4", description: "This is detail4", order: 2,
valve: "D", task_id: t2.id, done: false, cell: "cell2", record_id: r4.id)
t3 = Task.create(title: "Task3", cell: "cell1", done: false)
Detail.create(title: "Detail5", description: "This is detail5", order: 1,
valve: "A", task_id: t3.id, done: false, cell: "cell1", record_id: r1.id)
Detail.create(title: "Detail6", description: "This is detail6", order: 2,
valve: "B", task_id: t3.id, done: false, cell: "cell1", record_id: r2.id)
t4 = Task.create(title: "Task4", cell: "cell2", done: false)
Detail.create(title: "Detail7", description: "This is detail7", order: 1,
valve: "C", task_id: t4.id, done: false, cell: "cell2", record_id: r3.id)
Detail.create(title: "Detail8", description: "This is detail8", order: 2,
valve: "D", task_id: t4.id, done: false, cell: "cell2", record_id: r4.id)
这些是我的控制器
class TasksController < ApplicationController
def index
@tasks = Task.all
end
def show
@task = Task.find(params[:id])
@details = @task.details
end
end
class DetailsController < ApplicationController
def index
@details = Detail.all
end
def show
@detail = Detail.find(params[:id])
@task = @detail.task
@details = @task.details
next_order = @detail.order + 1
@details.each do |d|
if d.order = next_order
@next_detail = d
end
end
end
end
class RecordsController < ApplicationController
def index
@records = Record.all
end
def show
@record = Record.find(params[:id])
@details = @record.details
end
def edit
@record = Record.find(params[:id])
end
def update
@record = Record.find(params[:id])
if @record.update_attributes(record_params)
redirect_to("/records")
else
render 'edit'
end
end
private
def record_params
params.require(:record).permit(:state)
end
end
这些是我的观点
细节/ index.html.erb
<div class="tasklist">
<h2>TaskList</h2>
<% @details.each do |d| %>
<div class="tasks">
<%= link_to d.title, detail_path(d) %>
</div>
<% end %>
</div>
细节/ show.html.erb
<h1><%= @detail.title %></h1>
<p><%= @detail.description %></p>
<%= link_to "back", task_path(@task) %>
<%= link_to "next", detail_path(@next_detail) %>
任务/ show.html.erb
<h2><%= @task.title %></h2>
<table>
<tbody>
<% @details.each do |d| %>
<tr>
<th><%= d.order %></th>
<td><%= link_to d.title, detail_path(d) %></td>
</tr>
<% end %>
</tbody>
</table>