gpt4 book ai didi

ruby-on-rails - Rails 4 - 在 View 中显示来自不同模型的值

转载 作者:太空宇宙 更新时间:2023-11-03 17:14:53 25 4
gpt4 key购买 nike

我有一个模型,所有权。所有权属于另一个称为团队的模型。团队拥有_许多所有权。所有权是从数据库中的团队表中索引“team_id”。

Ownerships 数据库表中的一列是“team_id”。在所有权的索引 View 中,我希望能够根据 team_id 显示 Team 表中 :name 列中的团队名称。我已经破解了几个小时但没有成功。

所有权控制者:

class OwnershipsController < ApplicationController
def index
@ownerships = Ownership.all
@team = Team.all
end
def show
@ownership = Ownership.find(params[:id])
end
end

我的 index.html.erb 所有权 View 的相关部分:

<% @ownerships.each do |ownership| %>
<tr>
<td><%= ownership.round %></td>
<td><%= ownership.pick %></td>
<td><%= ownership.team.team_id { |a| a.name } %></td>
#want to display team name above
</tr>
<% end %>

如有任何帮助,我们将不胜感激。

最佳答案

只需使用:

<td><%= ownership.team.name%></td> 

因为您已经在所有权和团队模型之间建立了关联。

关于ruby-on-rails - Rails 4 - 在 View 中显示来自不同模型的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32337843/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com