我有一個叫lastthree.html.erb
是針對所有制模型顯示數據頁。我有一個名爲_nextpick.html.erb
的部分,也適用於所有權模型。軌道4 - 同一頁面上兩個表,查詢第二表忽略
在我ownerships_controller.rb
,我有兩個定義:
def nextpick
@ownerships = Ownership.take(Rails.application.config.counter).last(1)
end
def lastthree
@ownerships = Ownership.take(Rails.application.config.counter - 1).last(3)
end
如果我讓nextpick
自己的頁面(nextpick.html.erb
),然後從菜單訪問它,它工作正常,我得到正確的結果。但是,如果我將它作爲lastthree.html.erb
的一部分進行渲染,我會得到lastthree
查詢的結果。它在頁面上的位置並不重要。
爲什麼nextpick
查詢被忽略?無論如何要克服這一點?
兩個頁面都使用相同的代碼來顯示數據:
<% @ownerships.each do |ownership| %>
<tr>
<td></td>
<td><%= ownership.round %></td>
<td><%= ownership.pick %></td>
<td><%= image_tag ownership.team.logo.url(:medium) %></td>
<td><%= ownership.team.name %></td>
<% if ownership.player_id == nil %>
<% else %>
<td><%= ownership.player.name %></td>
<% end %>
</tr>
<% end %>
「nextpick」在哪裏被調用?你是否期待'nextpick'被調用,因爲這是正在渲染的部分名稱?渲染局部不會導致任何控制器方法被調用。路由器決定調用哪個控制器方法,它將只調用一個控制器方法:匹配路由的方法。 –
非常有幫助,我認爲渲染部分會導致調用控制器。是否不可能在同一頁上有兩個(或多個)表使用不同的查詢從相同的數據庫表中繪製數據? – SeattleDucati