Question

I have just deployed a site through Capistrano.

Its all gone to plav except my css and js is not loading on the page, in the markup:

<link data-turbolinks-track="true" href="/assets/application.css" media="all" rel="stylesheet" />
  <script data-turbolinks-track="true" src="/assets/application.js"></script>

I have the above but nothing is being loaded and I get a 404 error.

Its in the staging.rb that I am most confused as this is what i am using for deployment at the moment.

I my config/application.rb I have this:

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 Forge
  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

    # Enable the asset pipeline
    config.assets.enabled = true

    config.nav_lynx.selected_class = 'current'

  end
end

In development.rb I have:

Forge::Application.configure do

  config.cache_classes = false

  config.eager_load = false

  config.consider_all_requests_local       = true
  config.action_controller.perform_caching = false

  config.action_mailer.raise_delivery_errors = false

  config.action_mailer.default_url_options = { :host => 'localhost:3000' }

  config.active_support.deprecation = :log

  # Raise an error on page load if there are pending migrations
  config.active_record.migration_error = :page_load

  config.assets.debug = true
end

In production.rb i have:

Forge::Application.configure do
  # Settings specified here will take precedence over those in config/application.rb.

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

  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 Rails's static asset server (Apache or nginx will already do this).
  config.serve_static_assets = false

  # 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 = false

  # Generate digests for assets URLs.
  config.assets.digest = true

  # Version of your assets, change this if you want to expire all your assets.
  config.assets.version = '1.0'


  # Set to :debug to see everything in the log.
  config.log_level = :info

  config.i18n.fallbacks = true

  config.active_support.deprecation = :notify

  config.log_formatter = ::Logger::Formatter.new
end

In staging.rb I have:

Forge::Application.configure do
  # Settings specified here will take precedence over those in config/application.rb.

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

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

  # Disable Rails's static asset server (Apache or nginx will already do this)
  config.serve_static_assets = false

  # Compress JavaScripts and CSS
  config.assets.compress = true

  # Don't fallback to assets pipeline if a precompiled asset is missed
  config.assets.compile = false

  # Generate digests for assets URLs
  config.assets.digest = true

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

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

  # Log the query plan for queries taking more than this (works
  # with SQLite, MySQL, and PostgreSQL)
  # config.active_record.auto_explain_threshold_in_seconds = 0.5

end

It seems that when I do cap staging deploy it compiles the assets but in my rails live app they are not being loaded.

can anyone point me in the right direction as to where I am going wrong, I am literally banging my head on a brick wall here.

Cheers

Was it helpful?

Solution

After assets:precompile, you need to restart the server in order for rails to start serving with digests since it looks for compiled assets on startup. In your case the app most likely started without any compiled assets. Capistrano does not restart your app out of the box.

Passenger can be restarted by touching the tmp/restart.txt file. To allow capistrano to restart passenger, create a custom rake task:

namespace :deploy do
  desc 'Restart passenger without service interruption (keep requests in a queue while restarting)'
  task :restart do
    on roles(:app) do
      execute :touch, release_path.join('tmp/restart.txt')
    end
  end
end

and hook it into your capistrano deploy.rb with:

after 'deploy:publishing', 'deploy:restart'

Additionally your rake task can be improved to check if your server is running and then return an exit code accordingly (useful when using jenkins/hudson to run cap deploy's). This will also restart passenger instantly as it restarts when the first request comes in after tmp/restart.txt was touched. Remember to replace "Some word on home page" with any word that can be found in your html.

namespace :deploy do
  desc 'Restart passenger without service interruption (keep requests in a queue while restarting)'
  task :restart do
    on roles(:app) do
      execute :touch, release_path.join('tmp/restart.txt')

      unless execute :curl, '-s -k --location localhost | grep "Some word on home page" > /dev/null'
        exit 1
      end
    end
  end
end
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top