[英]How can I render multiple scaffold table in rails?
我分别有三个不同的模型、控制器和视图。 它们是项目、阶段和任务。 我想在完成的 project_controller#show 中渲染阶段。 项目与阶段是一对多关系,任务与阶段是多对一关系。 我想渲染每个阶段,然后是他们的任务,等等。 我曾尝试这样做,但出现错误。
路由文件
resources :projects do
resources :stages do
resources :tasks
end
end
project_controller.rb
def show
@project = Project.includes(:stages).find(params[:id])
@stages = @project.stages
end
项目/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>
任务/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>
使用您拥有的嵌套资源路由,我猜到 tasks#index 的路径将是:
/tasks/:project_id/:stage_id/
为此,您需要始终提供 project_id 和 stage_id 以查看任务。 因此,您的 tasks#index 操作应如下所示:
def index
@project = Project.find(params[:project_id])
@stage = @project.stages.find(params[:stage_id])
@tasks = @stage.tasks
end
这样,在您的 tasks/index.html.erb 视图中,您将可以访问 @project 和 @stage 实例变量,并且您可以使用正确的 URL:
new_project_stage_task_url(project_id: @project.id, stage_id: @stage.id)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.