Rails display related posts by tags with images fr

2019-09-05 08:23发布

问题:

postI'm trying to retrieve related posts by tags by showing the attached image per related post, uploaded by using the carrierwave gem

Tags were written from scratch but similar to acts-as-taggable-on gem. Below solution picked from Rails count article per tag and related article by tag displays the related posts by title.

<% @posts.each do |post| %>

<% post.tags.each do |tag| %>

RELATED POSTS:
<% tag.posts.each do |related_post| %>
<%= link_to related_post.title + " , ", post %>
<% end %>
<% end %>
<% end %>

Now I want related posts to be displayed by image instead of title per above. Code displaying image for post is

<%= image_tag(post.cover.url(:thumb)) if post.cover? %>

How can I fit this in the RELATED POSTS above? I tried

<%= image_tag(related_post.cover.url(:thumb)) if post.cover? %>

but this throws:

NameError: undefined local variable or method 'related_post' for #<#:0x007fb67db38fb8>

Obviously related_post is not recorded in the carrierwave uploader model since it is different from the tags model. Any help please?