【问题标题】:Showing only one image out of many for a single post仅显示单个帖子中的多个图像中的一个
【发布时间】:2013-02-02 00:30:45
【问题描述】:

我正在使用回形针将多张图片上传到一个模型,我有一个包含许多照片的帖子模型。

当我在视图中显示这些并且有多个图像分配给帖子时,帖子会乘以照片数量,所以如果有三张图像,我会得到三个相同的帖子。我知道为什么会这样当我像这样遍历记录时

<ul>
 <% @tynewyddpost.each do |t| %>
<li>
 <% t.photos.each do |p| %>
<a class="photo" href="#"><%= image_tag p.avatar.url(:thumbnail_news_images) %></a>
<p><a href=""><%= t.title %></a></p>
<p class="date"><%= t.created_at %></p>
</li>
<% end %>
<% end %>
</ul>

如果有多个,我如何声明我只想显示一个,我需要查看哪种方法?

谢谢

【问题讨论】:

    标签: ruby-on-rails ruby-on-rails-3 associations paperclip models


    【解决方案1】:

    好的,找到答案了

    <ul>
     <% @tynewyddpost.each do |t| %>
     <li>
     <% single = t.photos.first %>#Added this above call for image
     <a class="photo" href="#"><%= image_tag(single.avatar.url(:thumbnail_news_images)) %></a>#call first image like this
     <p><a href=""><%= t.title %></a></p>
     <p class="date"><%= t.created_at %></p>
     </li>
    <% end %>
    </ul>
    

    【讨论】:

    • 有第一个、最后一个以及许多其他选项。如果第一个不是合理的解决方案,请告诉我。
    • 所有可用方法的任何文档?通读会很有用,谢谢
    • 你可以在 - Finder Methods
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-02-21
    • 1970-01-01
    • 2022-01-14
    • 1970-01-01
    • 1970-01-01
    • 2016-04-14
    相关资源
    最近更新 更多