Rails 实例变量在开发中显示但在 Heroku 中不显示

Rails instance variables display in development but not on Heroku

我正在开发和生产中使用 Rails 4.2.3 和 Postgres 数据库创建一个应用程序,并部署到 Heroku。我的开发环境和生产环境都植入了相同的数据。

我有三个表 - 城市、社区、场地。

class Venues < ActiveRecord::Base
  belongs_to :neighborhood
end
class Neighborhood < ActiveRecord::Base
  belongs_to :city
  has_many :venues
end
class City < ActiveRecord::Base
  has_many :neighborhoods
end

在 app/views/neighborhoods/show.html.erb 页面上,我想显示属于该街区的场馆。

app/controllers/neighborhoods_controller.rb show 动作是这样的:

def show
  @city = City.find(params[:city_id])
  @neighborhood = @city.neighborhoods.find(params[:id])
  @venues = Venue.where(neighborhood_id: @neighborhood)
end

社区路线嵌入城市路线中,但场馆是未嵌入的单独资源。

resources :cities do
  resources :neighborhoods
end
resources :venues

任何特定街区的街区显示页面都会显示街区名称、城市名称,然后通过@venues 实例变量检查是否有任何场所。如果是这样,它会遍历它们中的每一个,否则它会声明没有场地。 app/views/neighborhoods/show.html.erb

<h1><%= @neighborhood.hood_name + ', ' + @city.city_name %></h1>
<% if @venues.present? %>
  <h3>Venues</h3>
  <% @venues.each do |venue|  %>
    <p><%= link_to venue.venue_name, venue %></p>
  <% end %>        
<% else %>
  <p>There are no venues in this neighborhood</p>
<% end %>

这在我的 mac 开发模式下工作正常,但是当我将它加载到 Heroku 时,@venues 实例变量始终为 nil。我使用 irb 和控制台确认场地确实在 Heroku 的数据库中。他们只是没有被认出来。知道如何解决这个问题吗?日志没有提供任何线索。从 gem 的角度来看,除了 pg gem,在生产中我使用 gem 'rails_12factor', '0.0.3' 和 'puma','~> 2.12 .2'.

附加信息

我试图将一个简单的实例变量从控制器传递到城市 controller/show 页面和社区 controller/show 页面的显示页面。 app/controllers/cities_controller.rb

    def show
      @hellocity = "Hello from the show action in the cities controller."
    end

app/controllers/neighborhoods_controller.rb

    def show
      @hellohood = "Hello from the show action in the neighborhoods controller."
    end

然后在视图中我调用了每个视图: app/views/cities/show.html.erb <p><%= @hellocity %></p>

app/views/neighborhoods/show.html.erb <p><%= @hellohood %></p>

结果是在我的开发环境中,当我转到任何城市页面时,我都会收到 hellocity 消息,当我转到任何社区页面时,我都会收到 hellohood 消息。但是当我把它放在 Heroku 上时,我只会在城市页面上收到消息。实例变量未传递到 app/views/neighborhoods/show.html.erb 页面。 @neighborhoods 和@cities 正在传递,但其他任何内容(例如@hellohood 或@venues)都被阻止。很奇怪。

首先您需要确保您的 Heroku 数据库中有该数据。

我建议在 heroku 上使用 运行 rails 控制台并逐步调试您的数据。你确实从 params[:city_id]params[:id].

知道 city_idneighborhood_id

简单地 运行 这些行一行一行地检查每行的结果:

city = City.find(params[:city_id]) # does city exists?
neighborhood = city.neighborhoods.find(params[:id]) # does it have neighbors?
venues = Venue.where(neighborhood_id: neighborhood) # do you have venues?

我很确定您会找到丢失的数据。

实际上,您只需要检查您是否有该社区 ID 的场所。如果您没有遇到 "Page not found" 错误,那么您的 CityNeighborhood 已经存在。

结果: 我能够通过在 Heroku 上重置 Postgres 数据库来解决这个问题。我通过 运行 迁移删除了社区 table。
rails generate migration DropNeighborhoodsTable

然后填充迁移文件:

class DropNeighborhoodsTable < ActiveRecord::Migration
  def up
    drop_table :neighborhoods
  end
  def down
    raise ActiveRecord::IrreversibleMigration
  end
end

然后在Heroku上迁移数据库:
heroku run rake db:migrate

然后我重新生成了邻域 model/table,然后重新播种数据。但是在它起作用之前,我必须多次执行此过程。最后一次我将城市 table 从包含美国所有城市(~30,000)减少到仅包含本地城市(~100)。所以也许问题是我目前在 Heroku 上使用免费套餐,而且数据太多,所以它以某种方式阻塞了它。老实说我不知道​​。但是现在可以了。