【问题标题】:Link_to Rails using Foreign and Primary KeyLink_to Rails 使用外键和主键
【发布时间】:2015-03-16 08:58:53
【问题描述】:

我有两个表:事件和图像

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>

任何帮助将不胜感激!

【问题讨论】:

    标签: ruby-on-rails schema


    【解决方案1】:

    您可以像这样通过事件关联访问名称:

    <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>
    

    【讨论】:

      猜你喜欢
      • 2023-04-01
      • 1970-01-01
      • 1970-01-01
      • 2011-09-28
      • 1970-01-01
      • 2017-03-09
      • 2019-07-15
      • 2020-07-07
      相关资源
      最近更新 更多