我正在尝试创建一个名为杂志的静态页面,该页面按日期顺序显示两种不同的模型体验和文章。
然而,我遇到了一个奇怪的问题,体验模型在杂志页面上重复。
页面控制器:
class PagesController < ApplicationController
include Pagy::Frontend
def home
@experiences = Experience.order('date DESC').limit(1)
@articles = Article.order('date DESC').limit(1)
end
def magazine
@experiences = Experience.order('date DESC')
@articles = Article.order('date DESC')
@feed = (@experiences.to_a + @articles.to_a).sort_by(&:date)
end
end
静态杂志查看页面:
<% if @feed.any? %>
<% @feed.each do |feed| %>
<% if feed.is_a? Experience %>
<div id="articles" align="center">
<% @experiences.each do |experience| %>
<div class="article-widget">
<%= link_to experience do %>
<%= image_tag experience.picture_url, :class=> "img-rounded", width: '80%' if experience.picture.present? %>
<% end %>
<div class="article-top-left"><h3><%= experience.category %></h3></div>
<div class="article-bottom-left"><h4><%= experience.title %><br /> <%= experience.location %></h4></div>
</div>
<% end %>
</div>
<% else %>
<% if feed.is_a? Article %>
<div id="articles" align="center">
<% @articles.each do |article| %>
<div class="article-widget">
<%= link_to article do %>
<%= image_tag article.picture, :class=> "img-rounded", width: '80%' if article.picture.present? %>
<% end %>
<div class="article-top-left"><h3><%= article.category %></h3></div>
<div class="article-bottom-left"><h4>Around London with <%= article.title %><br /></h4></div>
</div>
<% end %>
</div>
<% end %>
<% end %>
<% end %>
<% end %>
我无法在控制器或视图页面中找到任何明显的东西来复制体验模型,但我对编码真的很陌生,所以如果有人能帮忙,我将不胜感激!非常感谢
1条答案
按热度按时间vlju58qv1#
问题是您要循环遍历组合提要,然后是每个提要项的
@experiences
或@articles
。如果这两个代码分支之间只有一个微小的区别,则可以大大简化此代码: