如何在一个HTML表中显示来自两个相关数据库表的信息

时间:2011-12-20 17:33:49

标签: ruby-on-rails if-statement views

我刚刚开始使用Ruby on Rails并习惯了我正在研究小型足球预测游戏的语言。游戏规则是每周从10个选定的比赛中,用户必须选择他们认为的结果(HOME / AWAY / DRAW),并为每个灯具分配1-10的值,每个号码只允许使用一次。

我的模型简要概述如下: 用户:用户名,电子邮件,encrypted_pa​​ssword,salt,created_at,updated_at TEAMS:name,created_at,updated_at FIXTURES:date,home_team,away_team,weekno,result,winner,created_at,updated_at GAMEWEEKS:数字,匹配,截止日期,created_at,updated_at 选择:userid,gameweekno,fixtureno,winner,value,created_at,updated_at

我设置了显示页面,以便显示所选周的灯具即17周灯具可通过/ gameweeks / 17 访问,我现在希望显示这些用户的选择固定装置旁边但遇到了一些困难。显示页面的代码如下:

<%= notice %>

<h1>Gameweek <%= @gameweek.number %></h1>

<p>
  <b>Number of matches:</b> 
  <%= @gameweek.matches %>
  <b>Deadline Date:</b>
  <%= @gameweek.deadline.strftime("%d/%m/%Y") %>
  <b>Deadline Time:</b>
  <%= @gameweek.deadline.strftime("%H:%M") %>
</p>

<table width="100">
  <tr>
    <th>Date</th>
    <th>Kick Off</th>
    <th>Home</th>
    <th></th>
    <th>Away</th>
    <th>Result</th>
    <th>Prediction</th>
    <th>Value</th>
  </tr>

<% Fixture.where(:weekno => @gameweek.number).each do |fixture| %> 
  <tr>
    <td width="10"><%= fixture.date.strftime("%d/%m/%Y")%></td>
    <td width="10"><%= fixture.date.strftime("%H:%M") %></td>
    <td width="80"><%= fixture.home_team %></td> 
    <td width="10">Vs.</td>
    <td width="80"><%= fixture.away_team %></td>
    <td width="10"><%= fixture.result %></td>
    <% Selection.where(:userid => current_user).each do |fixture| %>
    <td width="10"><%= selection.winner %></td>
    <td width="10"><%= selection.value %></td>
  </tr>
<% end %>
</table>


<%= link_to 'Edit', edit_gameweek_path(@gameweek) %> |
<%= link_to 'Back', gameweeks_path %>

<h1>Gameweek <%= @gameweek.number %></h1> <p> <b>Number of matches:</b> <%= @gameweek.matches %> <b>Deadline Date:</b> <%= @gameweek.deadline.strftime("%d/%m/%Y") %> <b>Deadline Time:</b> <%= @gameweek.deadline.strftime("%H:%M") %> </p> <table width="100"> <tr> <th>Date</th> <th>Kick Off</th> <th>Home</th> <th></th> <th>Away</th> <th>Result</th> <th>Prediction</th> <th>Value</th> </tr> <% Fixture.where(:weekno => @gameweek.number).each do |fixture| %> <tr> <td width="10"><%= fixture.date.strftime("%d/%m/%Y")%></td> <td width="10"><%= fixture.date.strftime("%H:%M") %></td> <td width="80"><%= fixture.home_team %></td> <td width="10">Vs.</td> <td width="80"><%= fixture.away_team %></td> <td width="10"><%= fixture.result %></td> <% Selection.where(:userid => current_user).each do |fixture| %> <td width="10"><%= selection.winner %></td> <td width="10"><%= selection.value %></td> </tr> <% end %> </table> <%= link_to 'Edit', edit_gameweek_path(@gameweek) %> | <%= link_to 'Back', gameweeks_path %>

有关如何正确调用相应夹具的选择值以确保这是current_user的提示?

1 个答案:

答案 0 :(得分:1)

这不一定是执行此操作的最佳方式(视图中有很多逻辑,但您可能希望将上述代码部分更改为:

<% Fixture.where(:weekno => @gameweek.number).each do |fixture| %> 
  <tr>
    <td width="10"><%= fixture.date.strftime("%d/%m/%Y")%></td>
    <td width="10"><%= fixture.date.strftime("%H:%M") %></td>
    <td width="80"><%= fixture.home_team %></td> 
    <td width="10">Vs.</td>
    <td width="80"><%= fixture.away_team %></td>
    <td width="10"><%= fixture.result %></td>
    <% Selection.where(:userid => current_user.id, :fixtureno => fixture.id).each do |selection| %>
      <td width="10"><%= selection.winner %></td>
      <td width="10"><%= selection.value %></td>
    <% end %>
  </tr>
<% end %>

我必须纠正一些基本的语法问题。同样,你在rails中获得的越高级,你就越了解逻辑应该驻留在哪里,并且希望得知这不是解决这个问题的最有效方法。

干杯!