Cannot use db migrate when deploy to Heroku - ruby-on-rails

I tried to deploy my code to heroku. It only work with
config.assets.initialize_on_precompile = false
in my application.rb despite I'm using Rails 4.2.3. But when I tried to use
heroku run rake db:migrate
It cannot work correctly with error:
Name Error: Undefined local variable or method config for main:Object
/app/config/application.rb:7 in `'
Here is my Gemfile
source 'https://rubygems.org'
# Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
gem 'rails', '4.2.3'
group :production do
#Use PG
gem 'pg'
end
group :development do
# Use sqlite3 as the database for Active Record
gem 'sqlite3'
end
# Use SCSS for stylesheets
gem 'sass-rails', '~> 5.0.1'
# 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', '~> 2.0'
# bundle exec rake doc:rails generates the API under doc/api.
gem 'sdoc', '~> 0.4.0', group: :doc
# Use ActiveModel has_secure_password
# gem 'bcrypt', '~> 3.1.7'
# Use unicorn as the app server
# gem 'unicorn'
#Use Heroku
gem 'rails_12factor', group: 'production'
#Use bootstrap
gem 'bootstrap', '~> 4.0.0.alpha3'
gem 'autoprefixer-rails'
#Use Redcarpet
gem 'redcarpet'
#Use SQLite
#gem 'sqlite3', '1.3.11'
# Use Capistrano for deployment
# gem 'capistrano-rails', group: :development
# Use debugger
# gem 'debugger', group: [:development, :test]
# Windows does not include zoneinfo files, so bundle the tzinfo-data gem
gem 'tzinfo-data', platforms: [:mingw, :mswin]
And here is application.rb
require File.expand_path('../boot', __FILE__)
require 'rails/all'
# Require the gems listed in Gemfile, including any gems
# you've limited to :test, :development, or :production.
# Prevent initializing the application before assets are precompiled (required for heroku)
config.assets.initialize_on_precompile = false
Bundler.require(*Rails.groups)
module Blog
class Application < Rails::Application
# Settings in config/environments/* take precedence over those specified here.
# Application configuration should go into files in config/initializers
# -- all .rb files in that directory are automatically loaded.
# Set Time.zone default to the specified zone and make Active Record auto-convert to this zone.
# Run "rake -D time" for a list of tasks for finding time zone names. Default is UTC.
# config.time_zone = 'Central Time (US & Canada)'
# The default locale is :en and all translations from config/locales/*.rb,yml are auto loaded.
# config.i18n.load_path += Dir[Rails.root.join('my', 'locales', '*.{rb,yml}').to_s]
# config.i18n.default_locale = :de
end
end
Any solutions for my stuck point?
The log when I tried : git push heroku master with
config.assets.initialize_on_precompile = false
put in production.rb
http://i.stack.imgur.com/9rXp9.png
Sorry I cannot put img since I'm newbie.

Remove config.assets.initialize_on_precompile = false from application.rb
If you need to have this line in your rails application it should go in: app/config/environments/production.rb

Related

Ruby on Rails will not register active_admin upon server startup (http://localhost:3001/admin)

I am currently creating my first React-on-Rails project. I have an issue with loading the server when trying to run the RoR api.
Application.rb
require_relative 'boot'
require "rails"
# Pick the frameworks you want:
require "active_model/railtie"
require "active_job/railtie"
require "active_record/railtie"
require "active_storage/engine"
require "action_controller/railtie"
require "action_mailer/railtie"
require "action_view/railtie"
require "action_cable/engine"
require "sprockets/railtie"
require "rails/test_unit/railtie"
# Require the gems listed in Gemfile, including any gems
# you've limited to :test, :development, or :production.
Bundler.require(*Rails.groups)
module ListOfIngredients
class Application < Rails::Application
# Initialize configuration defaults for originally generated
Rails version.
config.load_defaults 5.2
# Settings in config/environments/* take precedence over those
specified here.
# Application configuration can go into files in
config/initializers
# -- all .rb files in that directory are automatically loaded
after loading
# the framework and any gems in your application.
# Only loads a smaller set of middleware suitable for API only
apps.
# Middleware like session, flash, cookies can be added back
manually.
# Skip views, helpers and assets when generating a new resource.
config.api_only = true
# Middleware for ActiveAdmin
config.middleware.use Rack::MethodOverride
config.middleware.use ActionDispatch::Flash
config.middleware.use ActionDispatch::Cookies
config.middleware.use ActionDispatch::Session::CookieStore
end
end
Gemfile.
source 'https://rubygems.org'
git_source(:github) { |repo| "https://github.com/#{repo}.git" }
ruby '2.5.1'
# Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
gem 'rails', '~> 5.2.0'
# Use Puma as the app server
gem 'puma', '~> 3.11'
# Build JSON APIs with ease. Read more:
https://github.com/rails/jbuilder
# gem 'jbuilder', '~> 2.5'
# Use Redis adapter to run Action Cable in production
# gem 'redis', '~> 4.0'
# Use ActiveModel has_secure_password
# gem 'bcrypt', '~> 3.1.7'
# Use ActiveStorage variant
# gem 'mini_magick', '~> 4.8'
# Use Capistrano for deployment
# gem 'capistrano-rails', group: :development
# Reduces boot times through caching; required in config/boot.rb
gem 'bootsnap', '>= 1.1.0', require: false
# Use Rack CORS for handling Cross-Origin Resource Sharing
(CORS), making cross-origin AJAX possible
# gem 'rack-cors'
group :development, :test do
# Use sqlite3 as the database for Active Record
gem 'sqlite3'
# Call 'byebug' anywhere in the code to stop execution and get a
debugger console
gem 'byebug', platforms: [:mri, :mingw, :x64_mingw]
end
group :development do
gem 'listen', '>= 3.0.5', '< 3.2'
# 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
# Use postgres as the database for production
gem 'pg'
end
# ActiveAdmin
gem 'devise'
gem 'activeadmin'
# Windows does not include zoneinfo files, so bundle the tzinfo-
data gem
gem 'tzinfo-data', platforms: [:mingw, :mswin, :x64_mingw,
:jruby]
I have also set up two controllers as follows.
api_controller.rb
class ApiController < ActionController::API
end
application_controller.rb
class ApplicationController < ActionController::Base
protect_from_forgery with: :exception
end
When I am running rails on localhost 3001, I somehow still end up with the error message.
Sprockets::Rails::Helper::AssetNotFound in ActiveAdmin::Devise::Sessions#new
where are active admins assets supposed to go when rails 5 api doesn't contain an assets folder?
I'm pretty sure ActiveAdmin requires sprockets. Did you run?
rails generate devise:install
You might also need to run
rails generate devise:views
For more info on vies see the devise wiki
And perhaps a look at this answer and this one too
UPDATE:
If you are following instructions at this tutorial you might wanna try to comment out this line in application.rb
# require "sprockets/railtie"

dotenv varibles are not loading in new Rails 4 app

Ruby version 2.1.6p336
Rails version 4.2.1
Gemfile:
source 'https://rubygems.org'
gem 'dotenv-rails', :groups => [:development, :test]
gem 'zendesk_api', '~> 1.13.0'
# Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
gem 'rails', '4.2.1'
# Use sqlite3 as the database for Active Record
gem 'sqlite3'
# 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'
# 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
# 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
# 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'
# Spring speeds up development by keeping your application running in the background. Read more: https://github.com/rails/spring
gem 'spring'
end
config/.env:
ZD_EMAIL= "hermes#olympus.com"
ZD_API_TOKEN= "ἙρμῆςκριοφόροςἈργειφόντης"
ZD_DOMAIN= "olympus.zendesk.com"
config/application.rb:
require File.expand_path('../boot', __FILE__)
require 'rails/all'
# Require the gems listed in Gemfile, including any gems
# you've limited to :test, :development, or :production.
Bundler.require(*Rails.groups)
module ZendeskSupportPortal
class Application < Rails::Application
# Settings in config/environments/* take precedence over those specified here.
# Application configuration should go into files in config/initializers
# -- all .rb files in that directory are automatically loaded.
# Set Time.zone default to the specified zone and make Active Record auto-convert to this zone.
# Run "rake -D time" for a list of tasks for finding time zone names. Default is UTC.
# config.time_zone = 'Central Time (US & Canada)'
# The default locale is :en and all translations from config/locales/*.rb,yml are auto loaded.
# config.i18n.load_path += Dir[Rails.root.join('my', 'locales', '*.{rb,yml}').to_s]
# config.i18n.default_locale = :de
# Do not swallow errors in after_commit/after_rollback callbacks.
config.active_record.raise_in_transactional_callbacks = true
end
end
I'm not trying to load them for a production environment, I'm trying to load them the standard way, just for development. But when I start the rails console and check, for example, ENV['ZD_EMAIL'], it returns nil.
I've tried everything mentioned in these two issues (in dotenv repo):
https://github.com/bkeepers/dotenv/issues/144
https://github.com/bkeepers/dotenv/issues/191
This is my first time using dotenv. Although the installation instructions are simple, I must be making a rookie mistake. Please enlighten me. Thanks!
It was a rookie mistake. I should have put the .env file in the root directory instead of config/.env. Putting it in the root directory (per the installation instructions) solved the issue.

Heroku's rake task fails to precompile assets - NameError: uninitialized constant ValidatesTimeliness

When I push my Rails 4 application to Heroku, it generates an "assets:precompile rake task" failure:
remote: Running: rake assets:precompile
remote: rake aborted!
remote: NameError: uninitialized constant ValidatesTimeliness
It appears Heroku is unable to precompile the assets, because of an issue with the gem "ValidatesTimeliness." Based on Heroku's documentation on the Rails 4 asset pipeline, I ran
RAILS_ENV=production bundle exec rake assets:precompile
to see if the problem also occurs in my local environment. It does; I got the same error locally.
Here's the contents of my config/intializers/validates_timeliness.rb file:
ValidatesTimeliness.setup do |config|
# Extend ORM/ODMs for full support (:active_record, :mongoid).
# config.extend_orms = [ :active_record ]
#
# Default timezone
# config.default_timezone = :utc
#
# Set the dummy date part for a time type values.
# config.dummy_date_for_time_type = [ 2000, 1, 1 ]
#
# Ignore errors when restriction options are evaluated
# config.ignore_restriction_errors = false
#
# Re-display invalid values in date/time selects
# config.enable_date_time_select_extension!
#
# Handle multiparameter date/time values strictly
# config.enable_multiparameter_extension!
#
# Shorthand date and time symbols for restrictions
# config.restriction_shorthand_symbols.update(
# :now => lambda { Time.current },
# :today => lambda { Date.current }
# )
#
# Use the plugin date/time parser which is stricter and extendable
# config.use_plugin_parser = false
#
# Add one or more formats making them valid. e.g. add_formats(:date, 'd(st|rd|th) of mmm, yyyy')
# config.parser.add_formats()
#
# Remove one or more formats making them invalid. e.g. remove_formats(:date, 'dd/mm/yyy')
# config.parser.remove_formats()
#
# Change the amiguous year threshold when parsing a 2 digit year
# config.parser.ambiguous_year_threshold = 30
#
# Treat ambiguous dates, such as 01/02/1950, as a Non-US date.
# config.parser.remove_us_formats
end
Here's the contents of my gem file:
source 'https://rubygems.org'
gem 'rails', '4.1.8'
gem 'bootstrap-sass', '~> 3.2.0.0'
gem 'sass-rails', '~> 4.0.3'
gem 'uglifier', '>= 1.3.0'
gem 'coffee-rails', '~> 4.0.0'
gem 'jquery-rails'
gem 'turbolinks'
gem 'jbuilder', '~> 2.0'
group :doc do
# bundle exec rake doc:rails generates the API under doc/api.
gem 'sdoc', require: false
end
group :development do
gem 'spring'
gem 'simple_form'
gem 'bcrypt', '~> 3.1.7'
end
group :development, :test do
gem 'pg'
gem 'rspec-rails', '~> 3.1.0'
gem 'factory_girl_rails'
gem 'validates_timeliness', '~> 3.0'
gem 'validates_overlap'
end
group :test do
gem 'byebug'
gem 'capybara', '~> 2.4.3'
gem 'shoulda-matchers', '~> 2.7.0'
gem 'faker'
gem 'paper_trail', '~> 3.0.6'
gem 'database_cleaner', '~> 1.3.0'
gem 'launchy', '~> 2.4.2'
gem 'rspec-activemodel-mocks'
gem 'selenium-webdriver', '~> 2.43.0'
end
group :production do
gem 'pg'
gem 'rails_12factor'
end
My hunch is I may need to do something in validates_timeliness set-up file, but I'm not sure what to do, because the rake failure relates to a "NameError." I'd appreciate any advice and guidance on how I should proceed.
In your Gemfile you've declared the validates_timeliness gem in a development group.
In Production it isn't installed, so you are getting an error.
Try moving the validates_timeliness gem out of the group or, if you only need it in development, surround your initializer code with:
if Rails.env.development?
ValidatesTimeliness.setup do |config|
end
end

application.css.scss not compiled on Rails 4

I felt strange that why the style not loaded,
So I wrote wrong content intentionally on application.css.scss,
And precompile and load the page didn't give me any error.
I thought the application.css.scss file must not be loaded and compiled.
Unfortunately, it seems no to be.
application.css.scss
#i2dmport "compass";
/*
* This is a manifest file that'll be compiled into application.css, which will include all the files
* listed below.
*
* Any CSS and SCSS file within this directory, lib/assets/stylesheets, vendor/assets/stylesheets,
* or vendor/assets/stylesheets of plugins, if any, can be referenced here using a relative path.
*
* You're free to add application-wide styles to this file and they'll appear at the top of the
* compiled file, but it's generally better to create a new file per style scope.
*
*= require_self
*= require jquery.fileupload-ui
*= require dropzone/dropzone
*= require dropzone/basicdew
*= require_tree .
*/
#import "layout";
application.rb
config.autoload_paths += %W(#{Rails.root}/app/pdfs)
config.autoload_paths += %W(#{config.root}/lib/)
# Set Time.zone default to the specified zone and make Active Record auto-convert to this zone.
# Run "rake -D time" for a list of tasks for finding time zone names. Default is UTC.
# config.time_zone = 'Central Time (US & Canada)'
config.assets.paths << "#{Rails.root}/vendor/themes"
Gemfile (I not sure if there any conflict)
source 'http://rubygems.org'
# Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
gem 'rails', '4.1.5'
# Use postgresql as the database for Active Record
gem 'pg', '~> 0.17.1'
# Use SCSS for stylesheets
gem 'sass-rails', '~> 4.0.0'
# 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
# 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
gem "bootstrap-sass", "~> 3.1.1"
gem "compass-rails"
gem "therubyracer"
gem "execjs"
gem "hirb-unicode"
gem "devise", "~> 3.3.0"
gem "devise_ldap_authenticatable", "~> 0.6.1"
gem "net-ldap"
gem "will_paginate", "~> 3.0"
gem "rspec"
gem "awesome_print", require: "ap"
gem "tinymce-rails"
gem "simple_form"
gem "haml-rails"
gem "taps"
gem "analytics-ruby"
gem "bcrypt-ruby"
gem "bourbon"
gem 'cancancan', '~> 1.8'
gem "sunspot_rails"
gem "sunspot_solr"
gem "simple-navigation"
gem "simple-navigation-bootstrap"
#fast command
gem "spring", group: :development
#Handy tools for active record
gem "squeel", "~> 1.2.1" # Last officially released gem
# gem "squeel", :git => "git://github.com/activerecord-hackery/squeel.git" # Track git repo
gem 'prawn'
gem 'rb-readline', "~> 0.5.0.pre.1"
gem 'chartkick'
gem 'rails-erd', :group => :development
gem 'quiet_assets', group: :development
gem 'curb'
gem 'rest_client'
# only for assets not required in production mode
gem 'jquery-rails'
gem 'jquery-ui-rails'
gem 'jquery.fileupload-rails', "~> 1.10.0"
gem 'twitter-bootstrap-rails'
group :development do
gem "faker"
gem "rspec-rails"
gem "guard-rspec"
gem "pry"
gem "pry-remote"
gem "pry-nav"
gem 'pry-rescue'
gem 'pry-stack_explorer'
end
gem "paperclip", "~> 4.1"
gem 'dropzonejs-rails'
layout
!!!
%html
%head
= stylesheet_link_tag "application", media: "all", "data-turbolinks-track" => true
= stylesheet_link_tag params[:controller]
= javascript_include_tag "application", "data-turbolinks-track" => true
= javascript_include_tag params[:controller], "data-turbolinks-track" => true
i had this exact same problem and was driving me crazy. i had a app/assets/stylesheets/mycustom_stylesheet.css.scss with the styles that loaded fine in development but when i deployed to production on heroku, none of my styles appeared. i solved it by placing the file custom.css in my vendor/assets/stylesheets directory with this content:
/*
*= require mycustom_stylesheet
*/
i also added this line to my initializers/assets.rb file although i'm not sure its necessary.
Rails.application.config.assets.precompile += %w( mycustom_stylesheet.css )
May be you have an "application.css" file inside of your "/vendor/themes/" or "/lib" folder ?
What happens if you remove this line "config.assets.paths << "#{Rails.root}/vendor/themes" from your application.rb file?
Try running below command
RAILS_ENV=production rake assets:precompile
or
RAILS_ENV=development rake assets:precompile
Today I came across a similar problem that changes in application.css.scss does not update in page styling. Later I found out that is because there is an error in my .scss file, ul being misspelled as u1. After the error is fixed, the styling finally updates in page refreshes, with no need to restart rails server.

Installing Doorkeeper : Uninitialized Constant Error

I'm attempting to install Doorkeeper on a fresh rails (4.0.2) install. After I add the gem to the gemfile and install it, I try to run
rails generate doorkeeper:install
and get the following error:
/Users/mam8cc/Projects/doorkeeper/config/application.rb:10:in `<module:Doorkeeper>': uninitialized constant Doorkeeper::Rails::Application (NameError)
from /Users/mam8cc/Projects/doorkeeper/config/application.rb:9:in `<top (required)>'
from /Users/mam8cc/.rvm/gems/ruby-2.0.0-p247/gems/railties-4.0.2/lib/rails/commands.rb:43:in `require'
from /Users/mam8cc/.rvm/gems/ruby-2.0.0-p247/gems/railties-4.0.2/lib/rails/commands.rb:43:in `<top (required)>'
from bin/rails:4:in `require'
from bin/rails:4:in `<main>'
My applications.rb
require File.expand_path('../boot', __FILE__)
require 'rails/all'
# Require the gems listed in Gemfile, including any gems
# you've limited to :test, :development, or :production.
Bundler.require(:default, Rails.env)
module Doorkeeper
class Application < Rails::Application
# Settings in config/environments/* take precedence over those specified here.
# Application configuration should go into files in config/initializers
# -- all .rb files in that directory are automatically loaded.
# Set Time.zone default to the specified zone and make Active Record auto-convert to this zone.
# Run "rake -D time" for a list of tasks for finding time zone names. Default is UTC.
# config.time_zone = 'Central Time (US & Canada)'
# The default locale is :en and all translations from config/locales/*.rb,yml are auto loaded.
# config.i18n.load_path += Dir[Rails.root.join('my', 'locales', '*.{rb,yml}').to_s]
# config.i18n.default_locale = :de
end
end
And my Gemfile for posterity
source 'https://rubygems.org'
# Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
gem 'rails', '4.0.2'
# Use sqlite3 as the database for Active Record
gem 'sqlite3'
# Use SCSS for stylesheets
gem 'sass-rails', '~> 4.0.0'
# 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
gem 'doorkeeper'
# 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
# Use ActiveModel has_secure_password
# gem 'bcrypt-ruby', '~> 3.1.2'
# Use unicorn as the app server
# gem 'unicorn'
# Use Capistrano for deployment
# gem 'capistrano', group: :development
# Use debugger
# gem 'debugger', group: [:development, :test]
The issue is with your project having the same name as the package you are trying to install. Change the name of the project and it should work.

Resources