ruby-on-rails 显示两个模型的静态页面上的重复

pdsfdshx  于 2023-04-08  发布在  Ruby
关注(0)|答案(1)|浏览(116)

我正在尝试创建一个名为杂志的静态页面,该页面按日期顺序显示两种不同的模型体验文章
然而,我遇到了一个奇怪的问题,体验模型在杂志页面上重复。
页面控制器:

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

我无法在控制器或视图页面中找到任何明显的东西来复制体验模型,但我对编码真的很陌生,所以如果有人能帮忙,我将不胜感激!非常感谢

vlju58qv

vlju58qv1#

问题是您要循环遍历组合提要,然后是每个提要项的@experiences@articles

<% if @feed.any? %> # you don't need if since the loop will run 0 times if there are no records 
  <% @feed.each do |feed| %>
    <% if feed.is_a? Experience %>
      <div id="articles" align="center">
        <% @experiences.each do |experience| %> # <-- this is where it goes wrong

如果这两个代码分支之间只有一个微小的区别,则可以大大简化此代码:

<% @feed.each do |feed_item| %>
  <div id="articles" align="center">
    <div class="article-widget">
      <%= link_to feed_item do %>
        <%  if feed_item.picture %>
          <!-- You should really consider the accessibility impacts of this. -->
          <!-- see https://www.w3.org/WAI/tutorials/images/functional/ -->
          <%= image_tag feed_item.picture, 
                        class: "img-rounded", 
                        width: "80%",
                        alt: "A text which describes the link"
          %>
        <% else %>
          <!-- You should provide fallback content if there is no image! -->
        <% end %>
      <% end %>
      <div class="article-top-left">
        <h3><%= feed_item.category %></h3>
      </div>
      <div class="article-bottom-left">
        <!-- This could be refactored into a helper method --> 
        <% if feed_item.is_a? Article %>
          <h4>Around London with <%= feed_item.title %></h4>
        <% else %>
          <h4><%= feed_item.title %></h4>
        <% end %>
      </div>
    </div>
  </div>
<% end %>

相关问题