Link_to Rails 使用外键和主键
Link_to Rails using Foreign and Primary Key
我有两个表:事件和图像
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>
如有任何帮助,我们将不胜感激!
您可以像这样通过事件关联访问名称:
<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>
我有两个表:事件和图像
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>
如有任何帮助,我们将不胜感激!
您可以像这样通过事件关联访问名称:
<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>