ruby-on-rails - 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 之间发生了什么来制造这个故障?
解决方案
您注意到每个博客都属于一个支柱。您是否确保在博客模型中添加has_many :pillars
(或has_one :pillar
,取决于您的需要)?我可以看到您在开发中明确设置关系(通过种子或其他方式)的情况,这在生产中不起作用。
推荐阅读
- mysql - 从 Laravel 中的 MySQL 字段插入和检索信息
- mysql - 以下示例如何以有效的方式设计数据库
- c++ - 什么时候可以安全地重用来自可简单破坏的对象的内存而不进行清洗
- angularjs - angularJs Factory .then() 不是函数
- vue.js - 在 vue-grid-layout 项目中传递一个组件
- c++ - 使用 auto 关键字初始化 char 数组
- php - 电子邮件模板问题 - WP-Invoice - Wordpress
- coldfusion - fileExists() vs CFHTTP 检查远程文件
- oracle - 使用受限选择 oracle 创建光标后插入数据
- python - 如何在 Python 中打开/转换 CSV,使字符串不是 unicode?