I am creating an app with Rails 4.2.3 and Postgres database in development and production, deploying to Heroku. Both my dev and production environment are seeded with the same data.
I have three tables - Cities, Neighborhoods, Venues.
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
On the app/views/neighborhoods/show.html.erb page I want to display the Venues that belong to that neighborhood.
The app/controllers/neighborhoods_controller.rb show action is this:
def show
@city = City.find(params[:city_id])
@neighborhood = @city.neighborhoods.find(params[:id])
@venues = Venue.where(neighborhood_id: @neighborhood)
end
The neighborhood routes are embedded in the cities routes, but venues are a separate resource not embedded.
resources :cities do
resources :neighborhoods
end
resources :venues
The Neighborhood show page for any particular neighborhood displays the neighborhood name, city name, then checks if there are any venues via the @venues instance variable. If so it iterates over each of them, otherwise it states there are no 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 %>
This works fine on my mac in dev mode, but when I load it to Heroku the @venues instance variable is always nil. I confirmed using irb and the console that the venues are indeed in the database on Heroku. They just aren't being recognized. Any idea how to fix this? The logs don't give any clues. From a gem perspective, beside the pg gem, in production I am using gem 'rails_12factor', '0.0.3' and 'puma','~> 2.12.2'.
Additional Information
I tried to just pass a simple instance variable from the controller to the show page for both the cities controller/show page and neighborhood controller/show page. 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
Then in the views I called each: app/views/cities/show.html.erb
<%= @hellocity %>
app/views/neighborhoods/show.html.erb
<%= @hellohood %>
The result is in my development environment when I go to any city page I get the hellocity message and when I go to any neighborhood page I get the hellohood message. But when I put this on Heroku I only get the message on the city page. The instance variable is not getting passed to the app/views/neighborhoods/show.html.erb page. @neighborhoods and @cities are being passed but anything else (e.g., @hellohood or @venues) is being blocked. Very strange.
Aucun commentaire:
Enregistrer un commentaire