ruby-on-rails - Rails 实例变量在开发中显示,但在 Heroku 中不显示

标签 ruby-on-rails ruby postgresql heroku instance-variables

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

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

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 显示操作是这样的:

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

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

resources :cities do
  resources :neighborhoods
end
resources :venues

任何特定街区的街区显示页面都会显示街区名称、城市名称,然后通过@venues 实例变量检查是否有任何 field 。如果是这样,它会遍历它们中的每一个,否则它会声明没有 field 。 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 和控制台确认 field 确实在 Heroku 的数据库中。他们只是没有被认出来。知道如何解决这个问题吗?日志没有提供任何线索。从 gem 的角度来看,除了 pg gem,在生产中我使用 gem 'rails_12factor', '0.0.3' 和 'puma','~> 2.12.2'。

附加信息

我试图将一个简单的实例变量从 Controller 传递到城市 Controller /显示页面和社区 Controller /显示页面的显示页面。 应用程序/ Controller /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

然后在 View 中我调用了每个 View : 应用程序/views/cities/show.html.erb

<%= @hellocity %>

app/views/neighborhoods/show.html.erb

<%= @hellohood %>

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

最佳答案

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

我建议在 heroku 上运行 rails console 并逐步调试您的数据。您确实从 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 的场所。如果您没有遇到“找不到页面”错误,那么您的 CityNeighborhood 已经存在。

关于ruby-on-rails - Rails 实例变量在开发中显示,但在 Heroku 中不显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31714335/

相关文章:

sql - 如何在 PostgreSQL 和 Oracle 数据库中选择值作为列表?

ruby-on-rails - Rspec - 如何在每次测试后清理数据库

css - select2 Rails 下拉列表未正确渲染

ruby-on-rails - 正则表达式未按预期工作

ruby-on-rails - 必须将捆绑执行添加到 rake db :migrate

ruby - 未初始化常量 bigdecimal (nameerror)

mysql - 集成两个 Rails 应用程序数据库

ruby - 如何在 Ruby 中有效地迭代和更新大字符串?

javascript - 从 Postgres 读取 float 而不四舍五入

sql - 如何在 PostgreSQL 中有效地设置减法连接表?