首页 > 解决方案 > Rails 问题:@blogs.count 显示在本地但不在 Heroku 上

问题描述

在我的应用程序上,我有Pillar和的模型Blog。每blog belongs_to一个pillar. 我有以下逻辑,pillars#show以便我可以在该支柱的show页面上显示属于某个支柱的博客:

  def show
    @blogs = Blog.where(pillars_id: @pillar.id).limit(6)
  end

然后,在show.html.erb页面上,我有这个:

<% if @blogs.count != 0 %>
  <div class="row blue-stripe">
    <div class="container">
      <h2 class="text-center">Blogs about <%= @pillar.name %></h2>
      <% @blogs.each do |blog| %>
        <%= link_to blog_path(blog) do %>
          <div class="card blog-card col-xs-6 col-md-3">
              <%= image_tag blog.image.url, alt: blog.title %>
              <h3><%= blog.title %></h3>
              <p><%= blog.teaser %></p>
              <p><em><%= link_to "Read More", blog_path(blog) %>...</em></p>
          </div> <!-- card -->
        <% end %> <!-- link -->
      <% end %> <!-- blogs each -->
    </div> <!-- container -->
  </div> <!-- row -->
<% end %>

这在本地工作得很好,但是当我部署到 Heroku 时,整个部分都没有显示出来。

作为一个例子,我通过运行确认heroku run rails c,事实上,我正在检查的支柱属于博客。例如,一个博客的记录如下所示:

Blog Load (2.9ms) SELECT "blogs".* FROM "blogs" ORDER BY "blogs"."id" DESC LIMIT $1 [["LIMIT", 1]] => #Touring a house is kind of like to go on a第一...”,user_id:2,created_at:“2018-06-03 21:00:56”,updated_at:“2018-06-03 21:00:56”,image_file_name:“53600574_l_(裁剪).jpeg” , image_content_type: "image/jpeg", image_file_size: 569841, image_updated_at: "2018-06-03 21:00:56", slug: "6-keys-to-looking-at-houses", link_text: "下载可打印的家庭游记表”,link_filename:“app/assets/images/house_tour_notes.pdf”,pillars_id:3>

然而,当我去pillars/3博客的蓝色条纹(的内部if @blogs.count)没有出现。

谁能弄清楚localhost和 Heroku 之间发生了什么来制造这个故障?

标签: ruby-on-railsheroku

解决方案


您注意到每个博客都属于一个支柱。您是否确保在博客模型中添加has_many :pillars(或has_one :pillar,取决于您的需要)?我可以看到您在开发中明确设置关系(通过种子或其他方式)的情况,这在生产中不起作用。


推荐阅读