我再次面对着一些不应该在表面上如此困难的东西,但却把我逼疯了一个多小时左右。我有多个模型,我想把它们拉入一个索引视图中。我假设这是一个组合,但我似乎无法找到一个方法来做到这一点。
我对索引的看法是:
<% @tips.each do |tip| %>
<tr>
<td><%= tip.user_id %></td>
<td><%= tip.city_id # here I want to draw on the cities table to show city.name
%></td>
<td><%= tip.type_id # here I want to draw on the type table to show type.name
%></td>
<td><%= tip.place_id # here I want to draw on the place table to show place.name
%></td>
<td><%= tip.tip_desc %></td>
<td><%= link_to Show , tip %></td>
<td><%= link_to Edit , edit_tip_path(tip) %></td>
<td><%= link_to Destroy , tip, confirm: Are you sure? , method: :delete %></td>
</tr>
<% end %>
以下是模型:
class Tip < ActiveRecord::Base
belongs_to :user
belongs_to :city
belongs_to :place
end
class Place < ActiveRecord::Base
belongs_to :city
has_and_belongs_to_many :collections
has_many :tips
end
class City < ActiveRecord::Base
has_many :places
has_many :tips
end
任何帮助都会感激不尽!
提前感谢大家,
詹姆斯·詹姆斯