简体   繁体   中英

Rails 5 and Heroku: can't do “heroku run rake db:migrate”

I'm trying to deploy my app to Heroku. I'm in final step where I try to do heroku run rake db:migrate however there is this error in terminal:

rake aborted!
NameError: undefined local variable or method `precompile' for #<Myapp::Application:0x007fcd3325a938>
Did you mean?  precompiled_assets
/app/config/environments/production.rb:3:in `block in <top (required)>'
/app/vendor/bundle/ruby/2.3.0/gems/railties-5.0.1/lib/rails/railtie.rb:209:in `instance_eval'
/app/vendor/bundle/ruby/2.3.0/gems/railties-5.0.1/lib/rails/railtie.rb:209:in `configure'
/app/config/environments/production.rb:1:in `<top (required)>'
/app/vendor/bundle/ruby/2.3.0/gems/activesupport-5.0.1/lib/active_support/dependencies.rb:293:in `require'
/app/vendor/bundle/ruby/2.3.0/gems/activesupport-5.0.1/lib/active_support/dependencies.rb:293:in `block in require'
/app/vendor/bundle/ruby/2.3.0/gems/activesupport-5.0.1/lib/active_support/dependencies.rb:259:in `load_dependency'
/app/vendor/bundle/ruby/2.3.0/gems/activesupport-5.0.1/lib/active_support/dependencies.rb:293:in `require'
/app/vendor/bundle/ruby/2.3.0/gems/railties-5.0.1/lib/rails/engine.rb:600:in `block (2 levels) in <class:Engine>'
/app/vendor/bundle/ruby/2.3.0/gems/railties-5.0.1/lib/rails/engine.rb:599:in `each'
/app/vendor/bundle/ruby/2.3.0/gems/railties-5.0.1/lib/rails/engine.rb:599:in `block in <class:Engine>'
/app/vendor/bundle/ruby/2.3.0/gems/railties-5.0.1/lib/rails/initializable.rb:30:in `instance_exec'
/app/vendor/bundle/ruby/2.3.0/gems/railties-5.0.1/lib/rails/initializable.rb:30:in `run'
/app/vendor/bundle/ruby/2.3.0/gems/railties-5.0.1/lib/rails/initializable.rb:55:in `block in run_initializers'
/app/vendor/bundle/ruby/2.3.0/gems/railties-5.0.1/lib/rails/initializable.rb:44:in `each'
/app/vendor/bundle/ruby/2.3.0/gems/railties-5.0.1/lib/rails/initializable.rb:44:in `tsort_each_child'
/app/vendor/bundle/ruby/2.3.0/gems/railties-5.0.1/lib/rails/initializable.rb:54:in `run_initializers'
/app/vendor/bundle/ruby/2.3.0/gems/railties-5.0.1/lib/rails/application.rb:352:in `initialize!'
/app/vendor/bundle/ruby/2.3.0/gems/railties-5.0.1/lib/rails/railtie.rb:193:in `public_send'
/app/vendor/bundle/ruby/2.3.0/gems/railties-5.0.1/lib/rails/railtie.rb:193:in `method_missing'
/app/config/environment.rb:5:in `<top (required)>'
/app/vendor/bundle/ruby/2.3.0/gems/activesupport-5.0.1/lib/active_support/dependencies.rb:293:in `require'
/app/vendor/bundle/ruby/2.3.0/gems/activesupport-5.0.1/lib/active_support/dependencies.rb:293:in `block in require'
/app/vendor/bundle/ruby/2.3.0/gems/activesupport-5.0.1/lib/active_support/dependencies.rb:259:in `load_dependency'
/app/vendor/bundle/ruby/2.3.0/gems/activesupport-5.0.1/lib/active_support/dependencies.rb:293:in `require'
/app/vendor/bundle/ruby/2.3.0/gems/railties-5.0.1/lib/rails/application.rb:328:in `require_environment!'
/app/vendor/bundle/ruby/2.3.0/gems/railties-5.0.1/lib/rails/application.rb:448:in `block in run_tasks_blocks'
/app/vendor/bundle/ruby/2.3.0/gems/rake-12.0.0/exe/rake:27:in `<top (required)>'
/app/vendor/bundle/ruby/2.3.0/gems/bundler-1.13.7/lib/bundler/cli/exec.rb:74:in `load'
/app/vendor/bundle/ruby/2.3.0/gems/bundler-1.13.7/lib/bundler/cli/exec.rb:74:in `kernel_load'
/app/vendor/bundle/ruby/2.3.0/gems/bundler-1.13.7/lib/bundler/cli/exec.rb:27:in `run'
/app/vendor/bundle/ruby/2.3.0/gems/bundler-1.13.7/lib/bundler/cli.rb:332:in `exec'
/app/vendor/bundle/ruby/2.3.0/gems/bundler-1.13.7/lib/bundler/vendor/thor/lib/thor/command.rb:27:in `run'
/app/vendor/bundle/ruby/2.3.0/gems/bundler-1.13.7/lib/bundler/vendor/thor/lib/thor/invocation.rb:126:in `invoke_command'
/app/vendor/bundle/ruby/2.3.0/gems/bundler-1.13.7/lib/bundler/vendor/thor/lib/thor.rb:359:in `dispatch'
/app/vendor/bundle/ruby/2.3.0/gems/bundler-1.13.7/lib/bundler/cli.rb:20:in `dispatch'
/app/vendor/bundle/ruby/2.3.0/gems/bundler-1.13.7/lib/bundler/vendor/thor/lib/thor/base.rb:440:in `start'
/app/vendor/bundle/ruby/2.3.0/gems/bundler-1.13.7/lib/bundler/cli.rb:11:in `start'
/app/vendor/bundle/ruby/2.3.0/gems/bundler-1.13.7/exe/bundle:34:in `block in <top (required)>'
/app/vendor/bundle/ruby/2.3.0/gems/bundler-1.13.7/lib/bundler/friendly_errors.rb:100:in `with_friendly_errors'
/app/vendor/bundle/ruby/2.3.0/gems/bundler-1.13.7/exe/bundle:26:in `<top (required)>'
/app/bin/bundle:3:in `load'
/app/bin/bundle:3:in `<main>'
Tasks: TOP => db:migrate => environment
(See full trace by running task with --trace)

In my application.rb I have:

config.assets.precompile += [ 'appviews.css', 'cssanimations.css', ...#other stuff
config.assets.precompile += [ 'appviews.js', 'cssanimations.js', ...#other stuff

I tried to move these precompile lines to config/environmens/development.rb but it did not help. I'm a bit confused, how to fix this, please? Thank you!

Update

Here is what I have in production.rb

Rails.application.configure do

  RAILS_ENV=production bundle exec rake assets:precompile
  # Settings specified here will take precedence over those in config/application.rb.

  # Code is not reloaded between requests.
  config.cache_classes = true

  # Eager load code on boot. This eager loads most of Rails and
  # your application in memory, allowing both threaded web servers
  # and those relying on copy on write to perform better.
  # Rake tasks automatically ignore this option for performance.
  config.eager_load = true

  # Full error reports are disabled and caching is turned on.
  config.consider_all_requests_local       = false
  config.action_controller.perform_caching = true

  # Disable serving static files from the `/public` folder by default since
  # Apache or NGINX already handles this.
  config.public_file_server.enabled = ENV['RAILS_SERVE_STATIC_FILES'].present?

  # Compress JavaScripts and CSS.
  config.assets.js_compressor = :uglifier
  # config.assets.css_compressor = :sass

  # Do not fallback to assets pipeline if a precompiled asset is missed.
  config.assets.compile = true

  # `config.assets.precompile` and `config.assets.version` have moved to config/initializers/assets.rb

  # Enable serving of images, stylesheets, and JavaScripts from an asset server.
  # config.action_controller.asset_host = 'http://assets.example.com'

  # Specifies the header that your server uses for sending files.
  # config.action_dispatch.x_sendfile_header = 'X-Sendfile' # for Apache
  # config.action_dispatch.x_sendfile_header = 'X-Accel-Redirect' # for NGINX

  # Mount Action Cable outside main process or domain
  # config.action_cable.mount_path = nil
  # config.action_cable.url = 'wss://example.com/cable'
  # config.action_cable.allowed_request_origins = [ 'http://example.com', /http:\/\/example.*/ ]

  # Force all access to the app over SSL, use Strict-Transport-Security, and use secure cookies.
  config.force_ssl = true

  # Use the lowest log level to ensure availability of diagnostic information
  # when problems arise.
  config.log_level = :debug

  # Prepend all log lines with the following tags.
  config.log_tags = [ :request_id ]

  # Use a different cache store in production.
  # config.cache_store = :mem_cache_store

  # Use a real queuing backend for Active Job (and separate queues per environment)
  # config.active_job.queue_adapter     = :resque
  # config.active_job.queue_name_prefix = "adsales_#{Rails.env}"
  config.action_mailer.perform_caching = false

  # Ignore bad email addresses and do not raise email delivery errors.
  # Set this to true and configure the email server for immediate delivery to raise delivery errors.
  # config.action_mailer.raise_delivery_errors = false

  # Enable locale fallbacks for I18n (makes lookups for any locale fall back to
  # the I18n.default_locale when a translation cannot be found).
  config.i18n.fallbacks = true

  # Send deprecation notices to registered listeners.
  config.active_support.deprecation = :notify

  # Use default logging formatter so that PID and timestamp are not suppressed.
  config.log_formatter = ::Logger::Formatter.new

  # Use a different logger for distributed setups.
  # require 'syslog/logger'
  # config.logger = ActiveSupport::TaggedLogging.new(Syslog::Logger.new 'app-name')

  config.public_file_server.enabled = ENV['RAILS_SERVE_STATIC_FILES'].present?
    if ENV["RAILS_LOG_TO_STDOUT"].present?
    logger           = ActiveSupport::Logger.new(STDOUT)
    logger.formatter = config.log_formatter
    config.logger = ActiveSupport::TaggedLogging.new(logger)
    end

  # Do not dump schema after migrations.
  config.active_record.dump_schema_after_migration = false
end

I don't understand why error is pointing to Myapp::Application which is in myapp/config/application.rb

EDIT

RAILS_ENV=production bundle exec rake assets:precompile

You need to remove this line from your production.rb which is causing the issue.

Also, this line doesn't belong to this file. You need to run this command manually to precompile the assets. Heroku does this for you.


Configuration

The configuration is changed in rails 5

Add this in production.rb

# Do not fallback to assets pipeline if a precompiled asset is missed.
config.assets.compile = true

# `config.assets.precompile` and `config.assets.version` have moved to config/initializers/assets.rb

You need to add the following in config/initializers/assets.rb

Rails.application.config.assets.version = '1.0'

# Add additional assets to the asset load path
# Rails.application.config.assets.paths << Emoji.images_path

# Precompile additional assets.
# application.js, application.css, and all non-JS/CSS in app/assets folder are already added.
Rails.application.config.assets.precompile += %w( search.js )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM