RoR app: Problems deploying app to heroku - ruby-on-rails

I created an heroku app recently for an existing rails app I've been working on. I only have one html file that I'd like to see displayed on my browser. This file gets news stories from my database (sqlite3-- should technically work from a development standpoint. I've gotten it to work with the railstutorial, where I didn't configure postgres at all), along with their corresponding images, and spits them to the page. This works perfectly on localhost, but when I deploy using 'heroku open', all I see is a blank white page with the heading tag that I added at the very top of the page: 'Top stories from BBC'. It's as if the app doesn't read the eRB tags at all. Here's the code for my index view html.erb file:
<h1> Top stories from BBC <h1>
<% #stories.each do |story| %>
<% if story.image.nil? %>
<% # do nothing %>
<% else %>
<div>
<h3><%= story.name %></h3>
<p><%= story.summary %></p>
<a href = "<%= story.url %>" >
<img src="<%= story.image %>" />
</a>
</div>
<% end %>
<% end %>
Here's my gemfile:
source 'https://rubygems.org'
# Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
gem 'rails', '4.0.8'
gem 'feedjira'
gem 'mechanize'
gem 'nokogiri'
# Use sqlite3 as the database for Active Record
group :development, :test do
gem 'sqlite3', '1.3.8'
gem 'rspec-rails', '2.13.1'
end
group :test do
gem 'selenium-webdriver', '2.35.1'
gem 'capybara', '2.1.0'
end
# Use SCSS for stylesheets
gem 'sass-rails', '~> 4.0.2'
# Use Uglifier as compressor for JavaScript assets
gem 'uglifier', '>= 1.3.0'
# Use CoffeeScript for .js.coffee assets and views
gem 'coffee-rails', '~> 4.0.0'
# See https://github.com/sstephenson/execjs#readme for more supported runtimes
# gem 'therubyracer', platforms: :ruby
# Use jquery as the JavaScript library
gem 'jquery-rails'
# Turbolinks makes following links in your web application faster. Read more: https://github.com/rails/turbolinks
gem 'turbolinks'
# Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder
gem 'jbuilder', '~> 1.2'
group :doc do
# bundle exec rake doc:rails generates the API under doc/api.
gem 'sdoc', require: false
end
group :production do
gem 'pg', '0.15.1'
gem 'rails_12factor', '0.0.2'
end
# Use ActiveModel has_secure_password
# gem 'bcrypt', '~> 3.1.7'
# Use unicorn as the app server
# gem 'unicorn'
# Use Capistrano for deployment
# gem 'capistrano', group: :development
# Use debugger
# gem 'debugger', group: [:development, :test]
I keep using heroku and git commands such as :
git push
git push heroku
heroku run rake db:migrate
rake assets:precompile
git add . /git commit -m/etc.
git push heroku master
Even after trying the above in multiple different combinations, nothing seems to work. I would appreciate any feedback you guys can offer as to how I can get my index page to render properly over Heroku. It works perfectly over local host.
Thanks, your help will be greatly appreciated!

You've pushed your code to Heroku, but your Heroku database is empty. If you have data in your local database you want on Heroku just push!
heroku pg:push mylocaldb HEROKU_POSTGRESQL_MAGENTA --app sushi
Where:
mylocaldb is the name of your local databases
HEROKU_POSTGRESQL_MAGENTA is the name of your heroku database (find it from heroku pg:info -a myapp)
IMPORTANT: Heroku uses postgres, so you should be using postgres locally as well. Many of the commands stated here require you to have postgres installed on your machine.
Be sure to read through the very useful help pages Heroku has: https://devcenter.heroku.com/articles/heroku-postgresql#pg-push-and-pg-pull

Related

Rails foreign key issue in Heroku migrate with Postgres

I am trying to deploy a Rails project with Heroku.
In development I am running the Ruby project on AWS Educate Cloud9 and using Sqlite3 as the database.
I can successfully run the Rails project on AWS and migrations work fine also there
$ rails db:migrate
In Production the database is postgres.
This is the Gemfile
source 'https://rubygems.org'
git_source(:github) do |repo_name|
repo_name = "#{repo_name}/#{repo_name}" unless repo_name.include?("/")
"https://github.com/#{repo_name}.git"
end
# Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
gem 'rails', '~> 5.0.7', '>= 5.0.7.2'
# Use sqlite3 as the database for Active Record
#gem 'sqlite3', '~>1.3.0'
#gem 'devise' , '~>4.3.0'
gem 'devise', git: 'https://github.com/plataformatec/devise' #, ref: '88e9a85'
# Use Puma as the app server
gem 'puma', '~> 3.0'
# Use SCSS for stylesheets
gem 'sass-rails', '~> 5.0'
# Use Uglifier as compressor for JavaScript assets
gem 'uglifier', '>= 1.3.0'
# Use CoffeeScript for .coffee assets and views
gem 'coffee-rails', '~> 4.2'
# See https://github.com/rails/execjs#readme for more supported runtimes
# gem 'therubyracer', platforms: :ruby
# Use jquery as the JavaScript library
gem 'jquery-rails'
# Turbolinks makes navigating your web application faster. Read more: https://github.com/turbolinks/turbolinks
gem 'turbolinks', '~> 5'
# Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder
gem 'jbuilder', '~> 2.5'
group :development, :test do
# Call 'byebug' anywhere in the code to stop execution and get a debugger console
gem 'byebug', platform: :mri
gem 'sqlite3', '~>1.3.0'
end
group :development do
# Access an IRB console on exception pages or by using <%= console %> anywhere in the code.
gem 'web-console', '>= 3.3.0'
gem 'listen', '~> 3.0.5'
# Spring speeds up development by keeping your application running in the background. Read more: https://github.com/rails/spring
gem 'spring'
gem 'spring-watcher-listen', '~> 2.0.0'
end
group :production do
gem 'pg'
end
# Windows does not include zoneinfo files, so bundle the tzinfo-data gem
gem 'tzinfo-data', platforms: [:mingw, :mswin, :x64_mingw, :jruby]
Using,
$ bundle update
$ bundle install --without production
$ git add .
$ git commit -m "blah"
$ git push origin master
$ git push heroku master
$ heroku run rails db:migrate
This gives an error which tells me that there is an issue with a foreign key, but when I db:migrate in development there is no issue with a foreign key. I only seem to get this error when I migrate in Heroku using 'pg'.

pg::error error relations users already exists

I am trying to deploy my app to heroku. Everything works fine until I try to run "heroku run rake db:migrate". Then I get the following error (please open this image in new tab to see better:
I ran rake db:drop, rake db:create, and rake db:migrate locally. Then I pushed it to heroku and tried running "heroku run rake db:migrate" again, but same error. I know this has something to with the schema already having user table, but I don't see it on my files.
Gem file:
source 'https://rubygems.org'
# Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
gem 'rails', '4.2.6'
# Use sqlite3 as the database for Active Record
# Use SCSS for stylesheets
gem 'sass-rails', '~> 5.0'
# Use Uglifier as compressor for JavaScript assets
gem 'uglifier', '>= 1.3.0'
# See https://github.com/rails/execjs#readme for more supported runtimes
# gem 'therubyracer', platforms: :ruby
# Use jquery as the JavaScript library
gem 'jquery-rails'
# Turbolinks makes following links in your web application faster. Read more: https://github.com/rails/turbolinks
gem 'turbolinks'
# Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder
gem 'jbuilder', '~> 2.0'
# bundle exec rake doc:rails generates the API under doc/api.
gem 'sdoc', '~> 0.4.0', group: :doc
# for users
gem 'devise', '~> 4.2'
# Use ActiveModel has_secure_password
gem 'bcrypt', git: 'https://github.com/codahale/bcrypt-ruby.git', :require => 'bcrypt'
# Use Unicorn as the app server
# gem 'unicorn'
gem 'tzinfo-data'
# Use Capistrano for deployment
# gem 'capistrano-rails', group: :development
group :development, :test do
# Call 'byebug' anywhere in the code to stop execution and get a debugger console
gem 'byebug'
end
group :test do
# for seeing test covarage
gem 'simplecov', :require => false
end
group :development do
# Access an IRB console on exception pages or by using <%= console %> in views
gem 'web-console', '~> 2.0'
# Spring speeds up development by keeping your application running in the background. Read more: https://github.com/rails/spring
gem 'spring'
gem 'sqlite3'
end
# upload files
gem 'carrierwave', '~> 1.0'
# bootstraps
gem 'bootstrap-sass', '~> 3.3.4.1'
# Material Icons
gem 'material_icons'
# Material css
gem 'materialize-sass'
group :production do
gem 'pg', '0.15'
gem 'rails_12factor'
end
Is there something else I have to do or am I missing something?
That means your table "users" already exists on heroku. Do you really want to delete all tables on heroku ? If so, you have you can do
heroku pg:reset DATABASE
https://devcenter.heroku.com/articles/heroku-postgresql#pg-reset
If you only want to make some changes on your tables you should really work with migrations like
add_column :users, :my_attribute, ...
removeColumn :users, :my_attribute_to_remove
PS: To drop, create, migrate the db locally will not have any effects at the db on heroku.

Uninitialized constant in Rails from Gem

I am fairly new to rails and am chasing my tail trying to get a Rails app to run on my server. Locally on my Mac, everything is working fine, however when I run it on the Ubuntu server, I'm getting a NameError (uninitialized constant Api::V1::TestController::Headless) error. I have updated bundler and the relevant gems as was suggested in other posts. I am certain that Headless is in my gem file, is up to date, and is install properly. I am using Headless in with Watir Webdriver. Any suggestions on what could be causing this error would be greatly appreciated.
Ruby Version: 2.2.3
The controller in question:
class Api::V1::TestController < ApplicationController
respond_to :json
def index
respond_with "test controller"
end
def create
event_submit(params[:json_event])
respond_to do |format|
if ( #log.present? )
format.json { render text: "Log: " + #log }
else
format.json { render text: "Error, no log" }
end
end
end
def nul_check(param)
param ||= "none"
return param
end
def event_submit(params)
#log = ""
#initialize the log
#log = ""
#set the browser that we will use to chrome for testing
#use headless
headless = Headless.new
headless.start
browser = Watir::browser.start 'www.google.com'
#log += browser.title
end
end
The Gemfile:
source 'https://rubygems.org'
# Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
gem 'rails', '4.2.4'
# Use postgresql as the database for Active Record
gem 'pg'
# Use SCSS for stylesheets
gem 'sass-rails', '~> 5.0'
# Use Uglifier as compressor for JavaScript assets
gem 'uglifier', '>= 1.3.0'
# Use CoffeeScript for .coffee assets and views
gem 'coffee-rails', '~> 4.1.0'
# Use jquery as the JavaScript library
gem 'jquery-rails'
# Turbolinks makes following links in your web application faster. Read more: https://github.com/rails/turbolinks
gem 'turbolinks'
# Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder
gem 'jbuilder', '~> 2.0'
# bundle exec rake doc:rails generates the API under doc/api.
gem 'sdoc', '~> 0.4.0', group: :doc
#Api gems
gem 'active_model_serializers'
gem 'deathbycaptcha'
gem 'rspec'
gem 'responders', '~> 2.0'
#Driver gems
gem 'watir-extensions-element-screenshot'
gem 'selenium-webdriver'
gem 'chromedriver-helper'
gem 'headless'
gem 'nokogiri'
gem 'watir-webdriver'
group :development, :test do
gem 'sqlite3'
# Call 'byebug' anywhere in the code to stop execution and get a debugger console
gem 'byebug'
end
group :development do
# Access an IRB console on exception pages or by using <%= console %> in views
gem 'web-console', '~> 2.0'
# Spring speeds up development by keeping your application running in the background. Read more: https://github.com/rails/spring
gem 'spring'
end
gem 'unicorn'
The problem is that Rails looks for the Headless constant under the Api::V1::TestController namespace.
You should use ::Headless (kind of absolute path to constant).
The problem turned out to be server ownership. The user, rails in my case, did not have ownership of the gems since I had run bundle install under my own account. A chmod for the rails user took care of the problem!

Ruby on Rails upgrade Mongoid to version 5.0.0

I have a Rails application which was using Mongoid 4.0.2. I upgraded Mongoid to version 5.0.0 and start getting this error
....gems/2.2.0/gems/less-rails-2.7.0/lib/less/rails/railtie.rb:19:in `block in <class:Railtie>': undefined method `register_preprocessor' for nil:NilClass (NoMethodError)
How to fix this issue or is any manual how to upgrade Mongoid to version 5.0.0 ?
Gemfile
source 'https://rubygems.org'
# Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
gem 'rails', '4.2.0.beta4'
# Use SCSS for stylesheets
gem 'sass-rails', '~> 5.0.0.beta1'
# Use Uglifier as compressor for JavaScript assets
gem 'uglifier', '>= 1.3.0'
# Use CoffeeScript for .coffee assets and views
gem 'coffee-rails', '~> 4.1.0'
# See https://github.com/sstephenson/execjs#readme for more supported runtimes
# gem 'therubyracer', platforms: :ruby
# Use jQuery as the JavaScript library
gem 'jquery-rails', '~> 4.0.0.beta2'
# Turbolinks makes following links in your web application faster. Read more: https://github.com/rails/turbolinks
gem 'turbolinks'
# Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder
gem 'jbuilder', '~> 2.0'
# bundle exec rake doc:rails generates the API under doc/api.
gem 'sdoc', '~> 0.4.0', group: :doc
#new gems
#database
gem 'mongoid', '~> 5.0.0.beta'
gem "mongoid-paperclip", :require => "mongoid_paperclip"
gem 'bson_ext'
#stile
gem "therubyracer"
gem "less-rails" #Sprockets (what Rails 3.1 uses for its asset pipeline) supports LESS
gem "twitter-bootstrap-rails"
gem 'bootstrap_form'
gem 'bootstrap-sass', '~> 3.3.4'
gem 'bootswatch-rails'
gem 'simple_form'
gem 'mongo'
gem "figaro"
gem 'sidekiq'
gem 'sidekiq-status'
gem 'rubyzip'
#gem 'sidekiq_status'
#gem 'sidekiq-status'
gem 'sinatra', require: false
gem 'slim'
gem 'capistrano', '~> 3.1.0'
# rails specific capistrano funcitons
gem 'capistrano-rails', '~> 1.1.0'
gem 'capistrano-rails-console'
# integrate bundler with capistrano
gem 'capistrano-bundler'
# if you are using RBENV
gem 'capistrano-rbenv', "~> 2.0"
gem 'capistrano-sidekiq', github: 'seuros/capistrano-sidekiq'
gem 'capistrano-passenger'
gem 'utf8-cleaner'
gem "devise"
gem 'rollbar', '~> 1.5.3'
#gem 'bson', '~> 3.0'
# Use ActiveModel has_secure_password
# gem 'bcrypt', '~> 3.1.7'
# Use Unicorn as the app server
# gem 'unicorn'
# Use Capistrano for deployment
# gem 'capistrano-rails', group: :development
group :development, :test do
gem 'thin'
# Call 'byebug' anywhere in the code to stop execution and get a debugger console
gem 'byebug'
# Access an IRB console on exception pages or by using <%= console %> in views
gem 'web-console', '~> 2.0.0.beta4'
# Spring speeds up development by keeping your application running in the background. Read more: https://github.com/rails/spring
gem 'spring'
#new gems
gem "rspec-rails"
gem 'pry-rails'
gem 'pry-rescue'
# Access an IRB console on exception pages or by using <%= console %> in views
gem 'better_errors'
#gem 'mongoid-rspec', '~> 2.1.0'
end
It looks like the error has appeared again in less-rails. The error is not related to Mongoid at all. I started getting the error a couple of days ago on my development server. I decided to check Github to see if an issue was reported about this. I found the following issue addressing the error.
https://github.com/metaskills/less-rails/pull/112
I changed my Gemfile to the following as provided on the link which corrected the error.
gem "less-rails", :git => 'https://github.com/suzan2go/less-rails.git', :branch => 'fix-nomethoderror-for-sprockets3'
As of now this solution has not been applied to the gem. I guess it will be soon.
Most important thing to look for while upgrading to Mongoid 5 is mongoid.yml
From:
<%= rails_env %>:
sessions:
default:
database: <%= mongodb_database %>
hosts:
- <%= mongodb_staging_host %>
username: <%= mongodb_user %>
password: <%= mongodb_password %>
options:
read: :primary_preferred
To:
<%= rails_env %>:
clients:
default:
database: <%= mongodb_database %>
hosts:
- <%= mongodb_staging_host %>
options:
user: <%= mongodb_user %>
password: <%= mongodb_password %>
read:
mode: :primary_preferred
Change sessions to clients
Move username (will become user) and password into options
change read mode as shown, if you have any.
Bottomline is to check mongoid.yml thoroughly while comparing with this page
Here how I fixed this issue
Removed from Gemfile gem "less-rails"
From application.rb removed require "mongo"
In mongoid.yml changed sessions: to clients:

Bootstrap and Rails issue with Heroku Deployment

Im making a Ruby on Rails app. I've included the twitter-bootstrap rails gem. Having tested the app locally, I've attempted numerous times to deploy to Heroku. I first encountered this 500 error:
ActionView::Template::Error (images/apple-touch-icon-144x144-precomposed.png isn't precompiled)
I solved this initial problem, by adding this line to the config/environment/production.rb file.
config.assets.compile = true
Now, I'm trying to push my rails app to Heroku again but when I push to Heroku I'm getting this 500 error again. I have no idea how to solve this one:
ActionView::Template::Error (undefined local variable or method `bootstrap_flash' for#<#
77: </div><!--/span-->
79: <%= bootstrap_flash %>
78: <div class="span9">
80: <%= yield %>
app/views/layouts/application.html.erb:79:in `_app_views_layouts_application_html_erb___2619453860724409461_34912060'
81: </div>
82: </div>
Any help would be greatly appreciated!
This is my Gemfile:
source 'https://rubygems.org'
gem 'rails', '3.2.13'
# Bundle edge Rails instead:
# gem 'rails', :git => 'git://github.com/rails/rails.git'
gem 'pg'
gem 'devise'
# Gems used only for assets and not required
# in production environments by default.
group :assets do
gem 'sass-rails', '~> 3.2.3'
gem 'coffee-rails', '~> 3.2.1'
# See https://github.com/sstephenson/execjs#readme for more supported runtimes
# gem 'therubyracer', :platforms => :ruby
gem 'uglifier', '>= 1.0.3'
gem "therubyracer"
gem "less-rails"
gem 'twitter-bootstrap-rails', :git => 'http://github.com/seyhunak/twitter-bootstrap-rails.git'
end
gem 'jquery-rails'
I've tried rake assets:precompile and then upload to Heroku but still, no luck.
You seems to be having this previously reported issue. Try adding the following gem:
gem 'bootstrap_helper'
You can also add this File, as one of your helpers. That's what fixed for most of the folks on that thread.

Resources