【发布时间】:2020-01-23 08:43:16
【问题描述】:
我分别有三种不同的模型、控制器和视图。它们是项目、阶段和任务。我想在 project_controller#show 中渲染舞台已经完成。项目与阶段是一对多的关系,任务与阶段是多对一的关系。我想渲染每个阶段,然后是他们的任务,等等。我曾尝试这样做,但出现错误。
routes.rb
resources :projects do
resources :stages do
resources :tasks
end
end
projects_controller.rb
def show
@project = Project.includes(:stages).find(params[:id])
@stages = @project.stages
end
projects/show.html.erb
<%= link_to "Add Stage", new_project_stage_url(@project), :class=>"button primary small" %>
<br>
<div class="table-scroll">
<table>
<thead>
<tr>
<th>Stage</th>
</tr>
</thead>
<tbody>
<% @stages.each do |stage| %>
<tr>
<td><%= stage.stage %></td>
</tr>
<% end %>
</tbody>
</table>
</div>
tasks/index.html.erb(我想添加任务按钮,所有与阶段相关的任务都将呈现在表中的项目显示页面上)
<%= link_to 'New Task', new_project_stage_task_url, :class=>"button primary" %>
<div class="table-scroll">
<table>
<thead>
<tr>
<th>Task name</th>
<th>Planned start date</th>
<th>Planned end date</th>
</tr>
</thead>
<tbody>
<% @tasks.each do |task| %>
<tr>
<td><%= task.task_name %></td>
<td><%= task.planned_start_date %></td>
<td><%= task.planned_end_date %></td>
</tr>
<% end %>
</tbody>
</table>
</div>
【问题讨论】:
-
你得到的错误是什么?你可以在这里粘贴堆栈跟踪吗?
-
当我尝试将任务代码添加到项目显示页面时出现错误 - 没有路由匹配 {:action=>"new", :controller=>"tasks", :id=>"1" },缺少必需的键:[:project_id, :stage_id] "button primary" %>
-
哦,您必须将 project_id 和 stage_id 提供给 new_project_stage_task_url。例如:
new_project_stage_task_url(project_id: project.id, stage_id: stage.id) -
@NwochaAdim 仍然错误 - 未定义局部变量或方法“项目”。当我检查 @project.id 和 @stage.id 它给出了 id 错误
-
我会在下面发布答案
标签: ruby-on-rails