我有两个表:事件和图像
Events:
id
name
date
location
Images:
id
url (where it's hosted)
caption
event_id
事件有很多图像,但图像只有一个事件。因此,Event.id连接到Image.event_id。
我想创建一个页面,其中的图像以其事件的名称显示,并且我正在努力做到这一点。到目前为止,我只能在页面上显示带有图像的event_id(这不是首选):
<div class="home-container">
<% @single_image.each do |picture| %>
<%= image_tag picture.url, :class => "body-images" %>
<li><%= link_to picture.event_id, events_path(picture.event_id), :class => "id-link" %></li>
<% end %>
</div>
任何帮助将不胜感激!
答案 0 :(得分:2)
您可以通过事件关联访问该名称,如下所示:
<div class="home-container">
<% @single_image.each do |picture| %>
<%= image_tag picture.url, :class => "body-images" %>
<li><%= link_to picture.event.name, events_path(picture.event_id), :class => "id-link" %></li>
<% end %>
</div>