【问题标题】:Display Table in "Show" Page using the same related ID使用相同的相关 ID 在“显示”页面中显示表格
【发布时间】:2015-09-08 02:32:49
【问题描述】:

我想在完成添加某些内容时显示同一事物的其他相关项目。

我有一个普通的 crud 脚手架,所以当我添加一个与项目相关的“任务”时,我想在它重定向到“显示”时形成一个表格,显示同一相关项目的其他“任务” ...

show.html.erb

<table>
<thead>
<tr>
  <th>Seq</th>
  <th>Descr</th>
  <th>Seqpai</th>
  <th>Type</th>
  <th>Hour</th>
  <th>Pid</th>
  <th colspan="3"></th>
  </tr>
  </thead>

 <tbody>
 <% @lookup.each do |lookup| %>
  <tr>
    <td><%= task.seq %></td>
    <td><%= task.descr %></td>
    <td><%= task.seqpai %></td>
    <td><%= task.typo %></td>
    <td><%= task.hour %></td>
    <td><%= task.projeto.name %></td>
  </tr>
<% end %>
</tbody>

任务控制器 =>

def lookup
@taskete = Task.where(@projeto_id)
end

Tasks 属于 Projeto,Projeto 有很多 Tasks

【问题讨论】:

  • 对不起,最好的问题是如何,我如何把这个循环显示用户选择的指定“项目任务”?用户选择“项目#1”,然后在表格中显示“项目#1”的所有任务。

标签: ruby-on-rails ruby


【解决方案1】:

我不明白有什么问题。但是,如果您的控制器的源代码定义了名为“@taskete”的变量,那么在视图中您将迭代“@lookup”。

你的循环应该是:@taskete.each do |task|

【讨论】:

    【解决方案2】:

    任务属于projecto,projecto有很多任务。所以要显示当前任务的projecto的所有任务...

     <% @taskete.projecto.tasks.each do |task| %>
    

    如果您只想显示其他任务(即不在列表中显示当前任务,仅显示相关任务),您可能想做...

     <% @taskete.projecto.tasks.each do |task| %>
       <% next if @taskete == task %>
    

    ...如果集合中的任务是@tasketo,则会跳过当前循环

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-04-09
      • 1970-01-01
      • 1970-01-01
      • 2015-01-11
      • 2017-03-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多