unable to deploy rails app to heroku - ruby-on-rails

my rails app works in development mode on my mac. Whrn i try to compile into heroku i get errors on the command line
Cleaning up the bundler cache.
Removing bundler (1.3.2)
-----> Writing config/database.yml to read from DATABASE_URL
-----> Preparing app for Rails asset pipeline
Running: rake assets:precompile
rake aborted!
undefined method `[]' for nil:NilClass
/tmp/build_9afd2a8f-0be9-427c-82f9-5431cf3a30fc/config/initializers/devise.rb:234:in `block in <top (required)>'
/tmp/build_9afd2a8f-0be9-427c-82f9-5431cf3a30fc/vendor/bundle/ruby/2.0.0/gems/devise-3.1.2/lib/devise.rb:276:in `setup'
/
/tmp/build_9afd2a8f-0be9-427c-82f9-5431cf3a30fc/vendor/bundle/ruby/2.0.0/gems/railties-4.0.0.rc1/lib/rails/engine.rb:609:in `bl
/tmp/build_9afd2a8f-0be9-427c-82f9-5431cf3a30fc/vendor/bundle/ruby/2.0.0/gems/railties-4.0.0.rc1/lib/rails/application.rb:214:in `initialize!'
/tmp/build_9afd2a8f-0be9-427c-82f9-5431cf3a30fc/vendor/bundle/ruby/2.0.0/gems/railties-4.0.0.rc1/lib/rails/railtie/configurable.rb:30:in `method_missing'
/tmp/build_9afd2a8f-0be9-427c-82f9-5431cf3a30fc/config/environment.rb:5:in `<top (required)>'
/tmp/build_9afd2a8f-0be9-427c-82f9-5431cf3a30fc/vendor/bundle/ruby/2.0.0/gems/activesupport-4.0.0.rc1/lib/active_support/dependencies.rb:228:in `require'
/tmp/build_9afd2a8f-0be9-427c-82f9-5431cf3a30fc/vendor/bundle/ruby/2.0.0/gems/activesupport-
Tasks: TOP => environment
(See full trace by running task with --trace)
!
! Precompiling assets failed.
!
! Push rejected, failed to compile Ruby app
checked my devise.rb
the only line i added was
config.omniauth :facebook, FACEBOOK_CONFIG['facebook_api_key'], FACEBOOK_CONFIG['facebook_api_secret'], scope: "email, publish_actions"
and this is my gemfile - as you can see i have the production and development information included
group :development, :test do
gem 'sqlite3'
end
group :production do
gem 'pg'
gem 'rails_12factor'
end
as advised devise 230-238
# ==> OmniAuth
# Add a new OmniAuth provider. Check the wiki for more information on setting
# up on your models and hooks.
config.omniauth :facebook, FACEBOOK_CONFIG['facebook_api_key'], FACEBOOK_CONFIG['facebook_api_secret'], scope: "email, publish_actions"
# ==> Warden configuration
# If you want to use other strategies, that are not supported by Devise, or
# change the failure app, you can configure them inside the config.warden block.

You probably want to stop using this magick FACEBOOK_CONFIG variable and start using environment variables for heroku
config.omniauth :facebook, ENV['FACEBOOK_API_KEY'], ENV['FACEBOOK_API_SECRET'], scope: "email, publish_actions"
to get this ENV working on development easier please check:
https://github.com/bkeepers/dotenv
or
https://github.com/laserlemon/figaro

It looks like the issue is with Devise.
the error says
undefined method `[]' for nil:NilClass
If you look at the next line down it is coming from.
config/initializers/devise.rb:234
So the error is on line 234 in config/initializers/devise.rb.
Run the following command on your mac.
bundle exec rake assets:precompile
If you don't get the same error, then you know that it is an issue with your production environment.
Can you add what you have on line config/initializers/devise.rb:234

Obviously FACEBOOK_CONFIG that you're trying to use is nil.
First you have to set the two environment variables in your Heroku account:
$ heroku config:set FACEBOOK_API_KEY=your-key-here
$ heroku config:set FACEBOOK_API_SECRET=your-secret-here
Then verify these are set correctly:
$ heroku config:get FACEBOOK_API_KEY
$ heroku config:get FACEBOOK_API_SECRET
Finally update the device.rb file as follows:
config.omniauth :facebook, ENV['FACEBOOK_API_KEY'], ENV['FACEBOOK_API_SECRET'], scope: "email, publish_actions"
Now should be good to go.

Just update your facebook.yml file with same information for production. I guess you only have development.
/config/facebook.yml
development:
facebook_api_key: copy_from_here
facebook_api_secret: copy_2_from_here
production:
facebook_api_key: paste_to_here
facebook_api_secret: paste_2_here

Related

NameError: uninitialized constant Dotenv while pushing rails app to heroku

I'm using dotenv to store environment variables and ever since I include it in the gemfile I cannot push it to heroku. I'm getting the following error:-
remote: -----> Installing node-v6.10.0-linux-x64
remote: -----> Detecting rake tasks
remote: sh: 2: Syntax error: Unterminated quoted string
remote: sh: 2: Syntax error: Unterminated quoted string
remote: !
remote: ! Could not detect rake tasks
remote: ! ensure you can run `$ bundle exec rake -P` against your app
remote: ! and using the production group of your Gemfile.
remote: ! rake aborted!
remote: ! NameError: uninitialized constant Dotenv
remote: ! /tmp/build_5437bc300afb80cfa46b1111bb960f46/config/application.rb:17:in `<top (required)>'
remote: ! /tmp/build_5437bc300afb80cfa46b1111bb960f46/Rakefile:4:in `require_relative'
remote: ! /tmp/build_5437bc300afb80cfa46b1111bb960f46/Rakefile:4:in `<top (required)>'
This is how I'm including dotenv in my gemfile:-
gem 'dotenv-rails', :require => 'dotenv/rails-now'
I have tried adding the following in the application.rb file as well:-
Bundler.require(*Rails.groups)
Dotenv::Railtie.load
HOSTNAME = ENV['HOSTNAME']
still doesn't work.
I don't know if those two lines that say "unterminated quoted string" could be some unrelated issue leading to the dotenv not loading. I looked it up and checked heroku config to see if there was something amiss in the variables but they all seem fine. I was able to push before I added the dotenv to gemfile.
I tried running bundle install, restarting server, deleting gemfile.lock and running bundle install and I looked this issue up on here and tried solutions suggested in Can't push to Heroku because of DOTENV uninitialized constant error
Still no luck.
PS - I'm trying to implement recaptcha and it is suggested best practice to use dotenv to store the site_key and secret_key for recaptcha as env vars. Hence I'm trying to get this to work.
I faced the same issue and was able to solve it with a following.
1) Add dotenv-rails in the Gemfile only for specific environments:
# Gemfile
group :development, :test do
gem 'dotenv-rails'
end
2) And run Dotenv stuff only if the environment matches your Gemfile group:
# config/application.rb
Bundler.require(*Rails.groups)
if ['development', 'test'].include? ENV['RAILS_ENV']
Dotenv::Railtie.load
end
Try adding the gem to the production group.
group :production do
gem 'dotenv-rails'
end

Mongoid::Errors::NoSessionConfig in rails consolse

I clone rails app (our company project) and then install everything that related to mongo db and mongoid gem. Then I run the rake db:setup and then rails s. Its working fine, I can access all the sites and do everything. But I got problem in the rails console.
Whenever I do this:
rails c
then
User.first
I will get this error
Loading development environment (Rails 4.1.1) irb(main):001:0>
User.first Mongoid::Errors::NoSessionConfig: Problem: No
configuration could be found for a session named 'default'. Summary:
When attempting to create the new session, Mongoid could not find a
session configuration for the name: 'default'. This is necessary in
order to know the host, port, and options needed to connect.
Resolution: Double check your mongoid.yml to make sure under the
sessions key that a configuration exists for 'default'. If you have
set the configuration programatically, ensure that 'default' exists in
the configuration hash. from
/Users/rizalmuthi/.rbenv/versions/2.1.1/lib/ruby/gems/2.1.0/gems/mongoid-4.0.0.beta2/lib/mongoid/sessions/factory.rb:27:in
create' from
/Users/rizalmuthi/.rbenv/versions/2.1.1/lib/ruby/gems/2.1.0/gems/mongoid-4.0.0.beta2/lib/mongoid/sessions.rb:65:in
with_name' from
/Users/rizalmuthi/.rbenv/versions/2.1.1/lib/ruby/gems/2.1.0/gems/mongoid-4.0.0.beta2/lib/mongoid/sessions.rb:105:in
mongo_session' from
/Users/rizalmuthi/.rbenv/versions/2.1.1/lib/ruby/gems/2.1.0/gems/mongoid-4.0.0.beta2/lib/mongoid/sessions.rb:121:in
collection' from
/Users/rizalmuthi/.rbenv/versions/2.1.1/lib/ruby/gems/2.1.0/gems/mongoid-4.0.0.beta2/lib/mongoid/sessions/options.rb:161:in
method_missing' from
/Users/rizalmuthi/.rbenv/versions/2.1.1/lib/ruby/gems/2.1.0/gems/mongoid-4.0.0.beta2/lib/mongoid/contextual/mongo.rb:263:in
initialize' from
/Users/rizalmuthi/.rbenv/versions/2.1.1/lib/ruby/gems/2.1.0/gems/mongoid-4.0.0.beta2/lib/mongoid/contextual.rb:53:in
new' from
/Users/rizalmuthi/.rbenv/versions/2.1.1/lib/ruby/gems/2.1.0/gems/mongoid-4.0.0.beta2/lib/mongoid/contextual.rb:53:in
create_context' from
/Users/rizalmuthi/.rbenv/versions/2.1.1/lib/ruby/gems/2.1.0/gems/mongoid-4.0.0.beta2/lib/mongoid/contextual.rb:35:in
context' from
/Users/rizalmuthi/.rbenv/versions/2.1.1/lib/ruby/gems/2.1.0/gems/mongoid-4.0.0.beta2/lib/mongoid/contextual.rb:20:in
first' from
/Users/rizalmuthi/.rbenv/versions/2.1.1/lib/ruby/gems/2.1.0/gems/mongoid-4.0.0.beta2/lib/mongoid/findable.rb:122:in
first' from (irb):1 from
/Users/rizalmuthi/.rbenv/versions/2.1.1/lib/ruby/gems/2.1.0/gems/railties-4.1.1/lib/rails/commands/console.rb:90:in
start' from
/Users/rizalmuthi/.rbenv/versions/2.1.1/lib/ruby/gems/2.1.0/gems/railties-4.1.1/lib/rails/commands/console.rb:9:in
start' from
/Users/rizalmuthi/.rbenv/versions/2.1.1/lib/ruby/gems/2.1.0/gems/railties-4.1.1/lib/rails/commands/commands_tasks.rb:69:in
console' from
/Users/rizalmuthi/.rbenv/versions/2.1.1/lib/ruby/gems/2.1.0/gems/railties-4.1.1/lib/rails/commands/commands_tasks.rb:40:in
run_command!' from
/Users/rizalmuthi/.rbenv/versions/2.1.1/lib/ruby/gems/2.1.0/gems/railties-4.1.1/lib/rails/commands.rb:17:in
' from
/Users/rizalmuthi/.rbenv/versions/2.1.1/lib/ruby/gems/2.1.0/gems/activesupport-4.1.1/lib/active_support/dependencies.rb:247:in require' from
/Users/rizalmuthi/.rbenv/versions/2.1.1/lib/ruby/gems/2.1.0/gems/activesupport-4.1.1/lib/active_support/dependencies.rb:247:inblock in require' from
/Users/rizalmuthi/.rbenv/versions/2.1.1/lib/ruby/gems/2.1.0/gems/activesupport-4.1.1/lib/active_support/dependencies.rb:232:in load_dependency' from
/Users/rizalmuthi/.rbenv/versions/2.1.1/lib/ruby/gems/2.1.0/gems/activesupport-4.1.1/lib/active_support/dependencies.rb:247:inrequire' from
/Users/rizalmuthi/Documents/Sites/WORK/tapway/bin/rails:8:in <top
(required)>' from
/Users/rizalmuthi/.rbenv/versions/2.1.1/lib/ruby/gems/2.1.0/gems/activesupport-4.1.1/lib/active_support/dependencies.rb:241:inload' from
/Users/rizalmuthi/.rbenv/versions/2.1.1/lib/ruby/gems/2.1.0/gems/activesupport-4.1.1/lib/active_support/dependencies.rb:241:in block in load' from
/Users/rizalmuthi/.rbenv/versions/2.1.1/lib/ruby/gems/2.1.0/gems/activesupport-4.1.1/lib/active_support/dependencies.rb:232:inload_dependency' from
/Users/rizalmuthi/.rbenv/versions/2.1.1/lib/ruby/gems/2.1.0/gems/activesupport-4.1.1/lib/active_support/dependencies.rb:241:in load' from
/Users/rizalmuthi/.rbenv/versions/2.1.1/lib/ruby/2.1.0/rubygems/core_ext/kernel_require.rb:55:in
require' from
/Users/rizalmuthi/.rbenv/versions/2.1.1/lib/ruby/2.1.0/rubygems/core_ext/kernel_require.rb:55:in
require' from -e:1:in'irb(main):002:0>
And FYI, this is the mongoid.yml that I generated by run
rails g mongoid:config
mongoid.yml
development:
sessions:
default:
database: mongo_demo_development
hosts:
- localhost:27017
options:
options:
test:
sessions:
default:
database: mongo_demo_test
hosts:
- localhost:27017
options:
consistency: :strong
max_retries: 1
retry_interval: 0
I have been looking all over google and some blogs, could not figure it out how to fix this.
Besides that, we dont use the Rspec tho.
change your config/application.rb file:
replace it Bundler.require(*Rails.groups)
to it Bundler.require(*Rails.groups(assets: %w(development test)))
In the latest mongoid -5.0.0 version you can simply solve by placing the below code in application.rb
config.generators do |g|
g.orm :mongoid
end
In config/application.rb change
Bundler.require(*Rails.groups)
to:
Bundler.require(:default, Rails.env)
Rails is trying to eager load the group of gems by environment and the stock config/mongoid.yml file you get when you run rails g mongoid:config only gives you a test and development namespace. Even though you're only running development, it's looking at all the groups. If you add the production namespace to the yml file it will load but my answer above works without you needing to do that.
Just make sure that on application initialization you do the following (in Rails console)
Mongoid.load!("path/to/your/mongoid.yml")
For more details visit http://mongoid.org/en/mongoid/docs/installation.html
With MongoId version 5.x, You will change config in Rails application follow:
File: application.rb
# Add line.
config.generators do |g|
g.orm :mongoid
end
I was set it and it working in rails console.
See more at:
https://docs.mongodb.com/ecosystem/tutorial/mongoid-installation/#rails-applications
may be The problem is in the gem file, you did't mention the version of mongoid gem.
in gem file change
gem "mongoid"
to
gem "mongoid" , '~> 4.0.2'
and run bundle install
restart server and console.

Ruby Rails app: After deleting OmniAuth gem and omniauth.rb, OmniAuth unitialized constant and rake aborted when attempt deploy

Resolved --> use git commit -A or git commit . in correct directory
What happened before:
I included OmniAuth gem in Gemfile, created omniauth.rb in config/initializer folder, used $bundle install.
I realized I didn't want to use the OmniAuth gem so I removed its line from the Gemfile and deleted the omniauth.rb file.
I've also removed the following line from the config/application.rb file just in case:
config.assets.precompile += %w(*.png *.jpg *.jpeg *.gif)
I ran $bundle update in hopes of updating everything.
Now:
App does work on local server, but when I try to deploy to heroku with $git push heroku, the deployment aborts and part of the resulting message is:
Running: rake assets:precompile
rake aborted!
uninitialized constant OmniAuth
/tmp/build_34578113-b133-4234-b473-3cd6bc4872ca/config/initializer/omniauth.rb:1:in >`<top (required)>
Tasks: TOP => environment
(See full trace by running task with --trace)
I've checked and there is no omniauth.rb file in my config/initializer folder, and no swap versions either.
Does anyone know why the system still thinks OmniAuth is still there and what can I do to get rid of it? Might I have missed something? Does anyone have suggestions?

Heroku upload-Precompiling assets failed

I need help. When trying to upload my app to heroku, I get this error, anyone know why? A few was wrong. thanks
Using rake (10.1.0)
...
Using tlsmail (0.0.1)
Using uglifier (2.1.2)
Your bundle is complete! It was installed into ./vendor/bundle
-----> Writing config/database.yml to read from DATABASE_URL
-----> Preparing app for Rails asset pipeline
Running: rake assets:precompile
/tmp/build_e8889be5-168c-49ed-81e7-b71061fc82ee/vendor/bundle/ruby/1.9.1/gems/tlsmail-0.0.1/lib/net/smtp.rb:806: warning: already initialized constant SMTPSession
...
/tmp/build_e8889be5-168c-49ed-81e7-b71061fc82ee/vendor/bundle/ruby/1.9.1/gems/tlsmail-0.0.1/lib/net/pop.rb:702: warning: already initialized constant APOPSession
DEPRECATION WARNING: You have Rails 2.3-style plugins in vendor/plugins! Support for these plugins will be removed in Rails 4.0. Move them out and bundle them in your Gemfile, or fold them in to your app as lib/myplugin/* and config/initializers/myplugin.rb. See the release notes for more on this: http://weblog.rubyonrails.org/2012/1/4/rails-3-2-0-rc2-has-been-released. (called from <top (required)> at /tmp/build_e8889be5-168c-49ed-81e7-b71061fc82ee/Rakefile:7)
...
rake aborted!
could not connect to server: Connection refused
Is the server running on host "127.0.0.1" and accepting
TCP/IP connections on port 5432?
/tmp/build_e8889be5-168c-49ed-81e7-b71061fc82ee/vendor/bundle/ruby/1.9.1/gems/activerecord-3.2.12/lib/active_record/connection_adapters/postgresql_adapter.rb:1208:in `initialize'
/tmp/build_e8889be5-168c-49ed-81e7-b71061fc82ee/vendor/bundle/ruby/1.9.1/gems/activerecord-3.2.12/lib/active_record/connection_adapters/postgresql_adapter.rb:1208:in `new'
...
/tmp/build_e8889be5-168c-49ed-81e7-b71061fc82ee/vendor/bundle/ruby/1.9.1/gems/actionpack-3.2.12/lib/sprockets/assets.rake:29:in `block (2 levels) in <top (required)>'
Tasks: TOP => environment
(See full trace by running task with --trace)
!
! Precompiling assets failed.
From the Heroku docs:
This means that your app is attempting to connect to the database as part of rake assets:precompile. Because the config vars are not present in the environment, we use a placeholder DATABASE_URL to satisfy Rails.
To resolve this issue, ensure that the following line appears in your config/application.rb:
# config/application.rb
config.assets.initialize_on_precompile = false
Once added, commit your change and redeploy to Heroku – your assets should compile without your app attempting to connect to the database, which should resolve the error you're witnessing.
UPDATE:
Line 46 of your stacktrace includes the following message: Devise.secret_key was not set.
According to the author of Devise, José Valim, this issue can be resolved in the following manner:
Please add the following to your Devise initializer:
config.secret_key = '-- secret key --'
Alternatively, the following solution seems to have worked for a number of users:
I went to my routes.rb file and commented out the line devise_for :installs
Then I went back and reran rails generate devise:install. If that doesn't work, use the previous version of devise by editing your Gemfile's reference to Devise like this: gem 'devise', '3.0.3' and then follow the steps i mentioned above.
There a few things that solved this issue for me:
# config/application.rb
config.assets.initialize_on_precompile = false
Then, before I deployed, I compiled my assets locally and committed them:
RAILS_ENV=production bundle exec rake assets:precompile
Also, I installed this heroku add on, as was specified by the app I was launching (in my case, Spree commerce)
heroku labs:enable user-env-compile -a myapp
And of course, make sure your database.yml file is set to use adapter: postgresql.
Commit all of this, push to heroku, and hopefully it will launch. If you still cannot open your app, try looking at the Heroku logs: heroku logs -n 500
I still needed to migrate my database with heroku run rake db:migrate
when you are using github and you are pushing to heroku while you are in develop branch, dont do it, go to master branch and get the updates in the develop by git merge develop
after that,
rails precompile:assets
git add -A
git commit -m "Precompile assets"
git push heroku master
if you want to open the website that you deployed
heroku open
if nothing shows, migrate your database first by:
heroku run rails db:migrate
heroku open
I have failed Heroku proceompiling with same error message.
Carrierwave causes that because I have missed set up SECRET_KEY_BASE to Heroku setting.

Can't make calls to heroku CLI from within a Rake task without the Heroku gem in the Gemfile

I've had an app running on Heroku for a while now and on a recent deployment saw the message that the Heroku gem was deprecated in favor of the Heroku Toolbelt. With the toolbelt, all of my Heroku commands work fine from the command line but within a Rake task (which I have setup for deployments) I get the following errors:
$ rake deploy:staging
Everything up-to-date
/Users/aramisbear/.rvm/gems/ruby-1.9.3-p194#myapp/gems/bundler-1.2.0/lib/bundler/rubygems_integration.rb:147:in `block in replace_gem': heroku is not part of the bundle. Add it to Gemfile. (Gem::LoadError)
from /Users/aramisbear/.rvm/gems/ruby-1.9.3-p194#myapp/bin/heroku:18:in `<main>'
from /Users/aramisbear/.rvm/gems/ruby-1.9.3-p194#myapp/bin/ruby_noexec_wrapper:14:in `eval'
from /Users/aramisbear/.rvm/gems/ruby-1.9.3-p194#myapp/bin/ruby_noexec_wrapper:14:in `<main>'
/Users/aramisbear/.rvm/gems/ruby-1.9.3-p194#myapp/gems/bundler-1.2.0/lib/bundler/rubygems_integration.rb:147:in `block in replace_gem': heroku is not part of the bundle. Add it to Gemfile. (Gem::LoadError)
from /Users/aramisbear/.rvm/gems/ruby-1.9.3-p194#myapp/bin/heroku:18:in `<main>'
from /Users/aramisbear/.rvm/gems/ruby-1.9.3-p194#myapp/bin/ruby_noexec_wrapper:14:in `eval'
from /Users/aramisbear/.rvm/gems/ruby-1.9.3-p194#myapp/bin/ruby_noexec_wrapper:14:in `<main>'
The errors being shown are from this deployment rake task:
namespace :deploy do
desc "deploys to Production after uploading assets to S3"
task :production do
puts `git push heroku master`
puts `heroku run rake db:migrate --app myapp`
puts `heroku restart --app myapp`
end
desc "deploys to Staging after uploading assets to S3"
task :staging do
puts `git push staging staging:master`
puts `heroku run rake db:migrate --app myapp-staging`
puts `heroku restart --app myapp-staging`
end
end
The two heroku commands in each task are where the problem comes from. If I execute them from the command line, they work just fine though. I doubt it matters but I'm running OSX with RVM as well.
Any idea how to fix this? I realize it's not a huge deal since I can just run those additional command manually or alias them, but I'd just like to know why there's a problem in the first place.
The gem's version of the heroku command probably has a higher priority in your PATH than the system's.
I was able to reproduce this in a test project.
When I run which heroku, I see that the shell's choice is /home/justinf/.rvm/gems/ruby-1.9.3-p286/bin/heroku.
All it takes is a simple gem uninstall heroku, answering yes to deleting the executable.
which heroku now gives me /usr/bin/heroku, and my test.rb now completes with no error instead of crashing out with a bundler exception.

Resources