Rails display related posts by tags with images from carrierwave

為{幸葍}努か 提交于 2019-12-12 04:07:49

问题


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?

来源:https://stackoverflow.com/questions/36381471/rails-display-related-posts-by-tags-with-images-from-carrierwave

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!