I am working on a personal website Hosted in AWS. Using Ruby 1.9.3 Rails 4.1.0beta1. When I run the code locally, it works like a charm. However when I push to AWS, its missing either some JS files or some CSS files. I think it is missing JS files because if I remove the following from application.js
//= require_tree .
I can reproduce the issue locally.
Here is my Gem file:
source 'https://rubygems.org'
ruby '1.9.3'
#ruby-gemset=danielhamiltondotcom_rails_4_0
gem 'rails', '4.1.0.beta1'
group :development, :test do
gem 'rspec-rails', '2.13.1'
end
group :test do
gem 'selenium-webdriver', '2.35.1'
gem 'capybara', '2.1.0'
end
gem 'sass-rails', '4.0.1'
gem 'uglifier', '2.1.1'
gem 'coffee-rails', '4.0.1'
gem 'jquery-rails', '3.0.4'
gem 'turbolinks', '1.1.1'
gem 'jbuilder', '1.0.2'
group :doc do
gem 'sdoc', '0.3.20', require: false
end
group :production do
gem 'rails_12factor', '0.0.2'
end
Here is my production.rb file:
Rails.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
# Eager load code on boot. This eager loads most of Rails and
# your application in memory, allowing both thread 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
# Enable Rack::Cache to put a simple HTTP cache in front of your application
# Add `rack-cache` to your Gemfile before enabling this.
# For large-scale production use, consider using a caching reverse proxy like nginx, varnish or squid.
# config.action_dispatch.rack_cache = true
# Disable Rails's static asset server (Apache or nginx will already do this).
config.serve_static_assets = true
# 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
# 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'
# 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
# Force all access to the app over SSL, use Strict-Transport-Security, and use secure cookies.
# config.force_ssl = true
# Set to :debug to see everything in the log.
config.log_level = :info
# Prepend all log lines with the following tags.
# config.log_tags = [ :subdomain, :uuid ]
# Use a different logger for distributed setups.
# config.logger = ActiveSupport::TaggedLogging.new(SyslogLogger.new)
# Use a different cache store in production.
# config.cache_store = :mem_cache_store
# Enable serving of images, stylesheets, and JavaScripts from an asset server.
# config.action_controller.asset_host = "http://assets.example.com"
# Precompile additional assets.
# application.js, application.css, and all non-JS/CSS in app/assets folder are already added.
#config.assets.precompile += %w( search.js )
# 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 can not be found).
config.i18n.fallbacks = true
# Send deprecation notices to registered listeners.
config.active_support.deprecation = :notify
# Disable automatic flushing of the log to improve performance.
# config.autoflush_log = false
# Use default logging formatter so that PID and timestamp are not suppressed.
config.log_formatter = ::Logger::Formatter.new
config.assets.paths << Rails.root.join('app','assets','font')
config.assets.precompile += %w(.svg .eot .woff .ttf)
config.assets.precompile = ['*.js', '*.css', '*.css.erb']
end
Here is a snippet of the html.erb page.
<%= stylesheet_link_tag "application", media: "all",
"data-turbolinks-track" => false %>
<%= javascript_include_tag "application", "data-turbolinks-track" => false %>
I can't quite figure out which files are missing. Please let me know if you need more information. I have been looking around all over and haven't been able to resolve this issue.
I have tried this: Rails 4: assets not loading in production.
I was able to resolve this issue:
1) I had an extra ) in my css file. I removed that to address the compile error that AWS was giving.
2) In my Application.js file I manually required all JS files needed instead of just using the tree:
//= require jquery
//= require modernizr
//= require bootstrap
//= require jquery.fitvids
//= require jquery.easing.1.3
//= require twitter
//= require jquery.fancybox.pack
//= require jquery.fancybox-thumbs
//= require jquery.fancybox-media
//= require stellar
//= require nicescroll.min
//= require jquery.isotope.min
//= require custom
//= require custom-home
//= require portfolio
//= require jquery.flexslider
//= require retina
3) I added gem 'sprockets', '2.8' to my GemFile which i had previously omitted.
Try
RAILS_ENV=production bundle exec rake assets:precompile
Also check your prod server and make sure the asset files are there under public.
Related
Before a deployment, I ran bundle install and bundle update. Then my Rails version switched from 5.2.4 to 5.2.5, and several gems were upgraded too. Everything is still working perfectly in development environment.
But problems appeared after deploying to production environment. I have the following issue, which I think is related to assets. Any page I request, the following message ends up in the log file:
I, [2021-04-22T22:48:43.887632 #7620] INFO -- : Rendered users/index.html.erb within layouts/application (11.6ms)
I, [2021-04-22T22:48:44.149881 #7620] INFO -- : Completed 500 Internal Server Error in 294ms (ActiveRecord: 1.9ms)
F, [2021-04-22T22:48:44.150616 #7620] FATAL -- :
F, [2021-04-22T22:48:44.150738 #7620] FATAL -- : ActionView::Template::Error (application.css):
F, [2021-04-22T22:48:44.150992 #7620] FATAL -- :
9: <title><%= full_title(yield(:title)).html_safe %></title>
10:
11: <link href="/assets/oblique-4.1.1/styles/images/favicon.png" rel="shortcut icon">
12: <%= stylesheet_link_tag "application", media: "all", "data-turbolinks-track" => true %>
13: <%= javascript_include_tag "application", "data-turbolinks-track" => true %>
14: <%= csrf_meta_tags %>
15: <%= render "layouts/shim" %>
F, [2021-04-22T22:48:44.151084 #7620] FATAL -- :
F, [2021-04-22T22:48:44.151184 #7620] FATAL -- : app/views/layouts/application.html.erb:12:in `_app_views_layouts_application_html_erb__255798539_93007000'
app/controllers/users_controller.rb:22:in `block (2 levels) in index'
app/controllers/users_controller.rb:21:in `index'
This happens only in the production environment, of which configurationis given by the untouched production.rb file:
DataQualityManager::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
# Eager load code on boot. This eager loads most of Rails and
# your application in memory, allowing both thread 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
# Enable Rack::Cache to put a simple HTTP cache in front of your application
# Add `rack-cache` to your Gemfile before enabling this.
# For large-scale production use, consider using a caching reverse proxy like nginx, varnish or squid.
# config.action_dispatch.rack_cache = true
# Debug mode disables concatenation and preprocessing of assets.
# This option may cause significant delays in view rendering with a large
# number of complex assets.
# config.assets.debug = true
# Disable Rails's static asset server (Apache or nginx will already do this).
config.serve_static_assets = true
# Compress JavaScripts and CSS.
config.assets.js_compressor = Uglifier.new(harmony: true)
# config.assets.css_compressor = :sass --> automatically used due to sass-rails gem
# Do not fallback to assets pipeline if a precompiled asset is missed.
config.assets.compile = true
# 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'
# 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
# Force all access to the app over SSL, use Strict-Transport-Security, and use secure cookies.
config.force_ssl = false
# Set to :debug to see everything in the log.
config.log_level = :info
# Prepend all log lines with the following tags.
# config.log_tags = [ :subdomain, :uuid ]
# Use a different logger for distributed setups.
# config.logger = ActiveSupport::TaggedLogging.new(SyslogLogger.new)
# Use a different cache store in production.
# config.cache_store = :mem_cache_store
# Enable serving of images, stylesheets, and JavaScripts from an asset server.
# config.action_controller.asset_host = "http://assets.example.com"
# Precompile additional assets.
# application.js, application.css, and all non-JS/CSS in app/assets folder are already added.
config.assets.precompile += %w( search.js )
# 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 can not be found).
# config.i18n.fallbacks = true
# Send deprecation notices to registered listeners.
config.active_support.deprecation = :notify
# Disable automatic flushing of the log to improve performance.
# config.autoflush_log = false
# Use default logging formatter so that PID and timestamp are not suppressed.
config.log_formatter = ::Logger::Formatter.new
# Store uploaded files locally.
config.active_storage.service = :local
# Define connection to email services
config.action_mailer.default_url_options = { :host => 'l021000110029b.adr.admin.ch/' }
config.action_mailer.delivery_method = :smtp
config.action_mailer.perform_deliveries = true
config.action_mailer.raise_delivery_errors = false
config.action_mailer.default :charset => "utf-8"
end
The Gemfile contains:
source 'https://rubygems.org'
gem 'rails', '~> 5.2'
gem 'bootstrap-sass'
gem 'sassc-rails'
gem 'coffee-rails', '~> 5.0'
gem 'uglifier', '~> 4.0'
gem 'bcrypt', '~> 3.1'
gem 'jquery-rails'
gem 'turbolinks', '~> 5.2'
gem 'jbuilder', '~> 2.6'
gem 'will_paginate', '~> 3.1'
gem 'd3-rails'
gem 'js_cookie_rails'
gem 'httparty', '~> 0.14'
gem 'nested_form_fields'
gem 'pg'
gem 'deep_cloneable', '~> 3.0'
#gem 'acts-as-taggable-on', '~> 6.0'
gem 'tzinfo-data'
gem 'audited', '~> 4.9'
gem 'awesome_print' # Layout enhancement for Audited
# Generate SQL scripts via renderer
gem 'sql_query'
# Workflow support
gem 'workflow-activerecord', '>= 4.1', '< 6.0'
# Full-text search with Postgres
gem 'pg_search'
# Use Puma as the app server
gem 'puma', '~> 5.0'
# XLSX format support
gem 'write_xlsx'
gem 'write_xlsx_rails', git: 'https://github.com/fchampreux/write_xlsx_rails.git', branch: 'master'
gem 'roo'
# Authentication and Authorisations
gem 'devise'
gem 'devise-security'
gem 'email_address' # for email validation
gem 'cancancan', '~> 3.0'
# MarkDown parser
gem 'redcarpet', '~> 3.5'
# eMail notification support
gem 'mailgun-ruby', '~>1.1'
# gem for dev and test only
group :development, :test, :validation do
gem 'annotate', '~> 3.0'
gem 'byebug'
gem 'faker'
gem 'shoulda-matchers'
gem 'rails-controller-testing'
gem 'rspec-rails', '~> 4.0'
gem 'factory_bot_rails', '~> 6.0'
gem 'capybara', '~> 3.15'
gem 'database_cleaner-active_record'
gem 'selenium-webdriver', '~>3.140'
end
Can you help me understand what causes the failure after the upgrade?
Thanks a lot!
I'm deploying a rails app to Heroku, and the process runs smoothly but on running the app in a browser, javascripts and drop-downs do not work.
I thought the Turbolinks might be the problem but after i fixed it locally, it doesn't seem to reflect in Heroku.
i have tried restarting the app server on Heroku using
'heroku restart'
i tried precompiling the assets in development mode before deploying using
'heroku run rake assets:precompile -a rhino-base'
i tried inspecting elements using chrome>inspect>console but i only see 1 error yet ALL the js scripts don't work.
here is my GEM FILE
'source 'https://rubygems.org'
git_source(:github) { |repo| "https://github.com/#{repo}.git" }
ruby '2.4.5'
# Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
gem 'rails', '~> 5.2.1', '>= 5.2.1.1'
group :production do
gem 'pg'
gem 'rails_12factor'
end
group :development do
# Use mysql as the database for Active Record
gem 'mysql2', '>= 0.4.4', '< 0.6.0'
end
# Use Puma as the app server
gem 'puma', '~> 3.11'
# Use SCSS for stylesheets
gem 'sass-rails', '~> 5.0'
# Use Uglifier as compressor for JavaScript assets
gem 'uglifier', '>= 4.1.18'
# See https://github.com/rails/execjs#readme for more supported runtimes
gem 'duktape'
# Use CoffeeScript for .coffee assets and views
gem 'coffee-rails', '~> 4.2'
# Turbolinks makes navigating your web application faster. Read more: https://github.com/turbolinks/turbolinks
gem 'turbolinks', '~> 5'
gem 'jquery-turbolinks'
# Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder
gem 'jbuilder', '~> 2.5'
##################################################
gem 'jquery-rails'
gem 'jquery-slimscroll-rails','~> 1.0','>= 1.0.9'
gem 'jquery-migrate-rails', '~> 1.2', '>= 1.2.1'
##################################################
# 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
group :development, :test do
# 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
# Access an interactive console on exception pages or by calling 'console' anywhere in the code.
gem 'web-console', '>= 3.3.0'
end
group :test do
# Adds support for Capybara system testing and selenium driver
gem 'capybara', '>= 2.15'
gem 'selenium-webdriver'
# Easy installation and use of chromedriver to run system tests with Chrome
gem 'chromedriver-helper'
end
# Windows does not include zoneinfo files, so bundle the tzinfo-data gem
gem 'tzinfo-data', platforms: [:mingw, :mswin, :x64_mingw, :jruby]'
heres my application.js FILE '
// This is a manifest file that'll be compiled into application.js, which will include all the files
// listed below.
// Any JavaScript/Coffee file within this directory, lib/assets/javascripts, or any plugin's
// vendor/assets/javascripts directory can be referenced here using a relative path.
// It's not advisable to add code directly here, but if you do, it'll appear at the bottom of the
// compiled file. JavaScript code in this file should be added after the last require_* statement.
// Read Sprockets README (https://github.com/rails/sprockets#sprockets-directives) for details
// about supported directives.
//
//= require jquery
//= require jquery.turbolinks
//= require rails-ujs
//= require activestorage
//= require_tree .
//= require turbolinks'
here is my production.rb FILE
Rails.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
# 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
# Ensures that a master key has been made available in either ENV["RAILS_MASTER_KEY"]
# or in config/master.key. This key is used to decrypt credentials (and other encrypted files).
# config.require_master_key = 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
# Store uploaded files on the local file system (see config/storage.yml for options)
config.active_storage.service = :local
# 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 = "rhinoBaseSite_#{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')
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 expected that since everything works in development and production, it should also work on deployment.
so anyone else with this problem, here is what worked for me. Not all javascript files play nice when compiled in a single space. Compile them separately.
in your
config/initializers/assets.rb file have this line for the specific script you would love to compile in a separate file
Rails.application.config.assets.precompile += %w( path-to-file-in-javascripts-folder/file-name.js path-to-file-in-stylesheet-folder/file-name.scss)
for example
Rails.application.config.assets.precompile += %w( plugins/fullcalendar/fullcalendar.js )
then update your application.js and remove
//= require tree
then update your app/views/layouts/application.html.erb file and add the precompile asset path adding the /assets before the location. Like this
<%= javascript_include_tag '/assets/plugins/datatables/export/pdfmake.min' %>
now put all the files you had precompiled inside of the public/assets folder. then push to heroku.
I work with :
Rails version 4.2.3
Ruby version 2.2.1
I have managed to run my application in production doing these steps :
rake db:create RAILS_ENV=production
rake db:migrate RAILS_ENV=production
rake assets:precompile and rake assets:precompile RAILS_ENV=production
I had some troubles with my application.js and application.css files because all the require were not in the right orders. I had to put require_tree at the bottom and require jquery at the top directly followed by require jquery_ujs.
Now my files are the following :
application.js file :
//= require jquery
//= require jquery_ujs
//= require bootstrap
//= require dataTables/jquery.dataTables
//= require dataTables/extras/dataTables.tableTools
//= require_tree .
application.css file :
/*
*= require_self
*= require bootstrap
*= require dataTables/jquery.dataTables
*= require_tree .
*/
production.rb file :
Rails.application.configure do
# Force all access to the app over SSL, use Strict-Transport-Security,
# and use secure cookies.
config.force_ssl = false
# 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
# Enable Rack::Cache to put a simple HTTP cache in front of your application
# Add `rack-cache` to your Gemfile before enabling this.
# For large-scale production use, consider using a caching reverse proxy like
# NGINX, varnish or squid.
# config.action_dispatch.rack_cache = true
# Disable serving static files from the `/public` folder by default since
# Apache or NGINX already handles this.
config.serve_static_files = 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
# Asset digests allow you to set far-future HTTP expiration dates on all assets,
# yet still be able to expire them through the digest params.
config.assets.digest = true
# `config.assets.precompile` and `config.assets.version` have moved to config/initializers/assets.rb
# 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
# 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 = [ :subdomain, :uuid ]
# Use a different logger for distributed setups.
# config.logger = ActiveSupport::TaggedLogging.new(SyslogLogger.new)
# Use a different cache store in production.
# config.cache_store = :mem_cache_store
# Enable serving of images, stylesheets, and JavaScripts from an asset server.
# config.action_controller.asset_host = 'http://assets.example.com'
# 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
# Do not dump schema after migrations.
config.active_record.dump_schema_after_migration = false
end
development.rb file :
Rails.application.configure do
# Settings specified here will take precedence over those in config/application.rb.
# In the development environment your application's code is reloaded on
# every request. This slows down response time but is perfect for development
# since you don't have to restart the web server when you make code changes.
config.cache_classes = false
# Do not eager load code on boot.
config.eager_load = false
# Show full error reports and disable caching.
config.consider_all_requests_local = true
config.action_controller.perform_caching = false
# Don't care if the mailer can't send.
config.action_mailer.raise_delivery_errors = false
# Print deprecation notices to the Rails logger.
config.active_support.deprecation = :log
# Raise an error on page load if there are pending migrations.
config.active_record.migration_error = :page_load
# Debug mode disables concatenation and preprocessing of assets.
# This option may cause significant delays in view rendering with a large
# number of complex assets.
config.assets.debug = true
# Asset digests allow you to set far-future HTTP expiration dates on all assets,
# yet still be able to expire them through the digest params.
config.assets.digest = true
# Do not compress assets
# config.assets.compress = false
# Adds additional error checking when serving assets at runtime.
# Checks for improperly declared sprockets dependencies.
# Raises helpful error messages.
config.assets.raise_runtime_errors = true
# Raises error for missing translations
# config.action_view.raise_on_missing_translations = true
end
However all the javascript that I wrote is not working in production. I looked into my browser consol and my javascript is loaded I can see it in the DOM when I right click "inspect element".
So why my javascript works in development but is not working in production ?
I tried several options :
1st try : I add the line config.assets.precompile += %w( *.css *.js)
2nd try : config.assets.precompile += %w( *.css *scss *.js)
3rd try : I found that in config/initializers/assets.rb was a line of code very similar to what I tried in my 1st and 2nd try so I enabled this line of code in config/initializers/assets.rb : Rails.application.config.assets.precompile += %w( search.js ) but I still have nothing.
4th try : I read in another post on Stackoverflow that removing in my GemFile jquery-ui-rails could work but I still have no javascript
Here is my GemFile :
source 'https://rubygems.org'
# Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
gem 'rails', '4.2.3'
#To have charts on rails
gem 'chartkick'
#Turn into pdf
gem 'pdfkit'
#To read, write spreadsheets
gem 'spreadsheet', '~> 1.0.3'
#To import CSV file and spreadsheets
gem 'roo', '~> 2.1.0'
#To improve tables style
gem 'jquery-datatables-rails', '~> 3.3.0'
gem 'jquery-ui-rails'
#Pour qu'il fonctionne, le gem bcrypt doit ĂȘtre utilisĂ© sous sa version 3.1.9
gem 'bcrypt', '~>3.1.7'
gem 'bootstrap-sass', '3.2.0.0'
# Use mysql as the database for Active Record
gem 'mysql2'
# 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'
end
# Windows does not include zoneinfo files, so bundle the tzinfo-data gem
gem 'tzinfo-data', platforms: [:mingw, :mswin, :x64_mingw, :jruby]
Would anyone have an idea about what's happening ? Why my javascript is not working in production but works well in development ?
In production mode, it should server by web servers(Apache, Nginx) and will work. until that fix you can load all files in production rather loading a compressed single application.js and application.css file. For that in production.rb file disable the config variable. like below
config.serve_static_assets = false
The problem lies within your application.js and more specifically I believe where your bootstrap is being called. What fixed it for me was moving bootstrap to the bottom.
Asset pipeline is disabled in dev which is why you did not experience the same issues there.
An image was causing a problem on my heroku app so I changed config.assets.compile = false to config.assets.compile = true in production.rb. I then ran rake assets:precompile and pushed to the heroku server. Jquery works fine on the app website, but no longer works on my local copy. No errors are thrown in the javascript console. Here are some important files as they stand now.
Production.rb
Nonogrammed::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
# Defaults to nil and saved in location specified by config.assets.prefix
# config.assets.manifest = YOUR_PATH
# 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
# Force all access to the app over SSL, use Strict-Transport-Security, and use secure cookies.
# config.force_ssl = true
# See everything in the log (default is :info)
# config.log_level = :debug
# Prepend all log lines with the following tags
# config.log_tags = [ :subdomain, :uuid ]
# Use a different logger for distributed setups
# config.logger = ActiveSupport::TaggedLogging.new(SyslogLogger.new)
# Use a different cache store in production
# config.cache_store = :mem_cache_store
# Enable serving of images, stylesheets, and JavaScripts from an asset server
# config.action_controller.asset_host = "http://assets.example.com"
# Precompile additional assets (application.js, application.css, and all non-JS/CSS are already added)
# config.assets.precompile += %w( search.js )
# Disable delivery errors, bad email addresses will be ignored
# config.action_mailer.raise_delivery_errors = false
# Enable threaded mode
# config.threadsafe!
# 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
Application.js
//= require jquery
//= require jquery_ujs
//= require jquery.ui.core
//= require jquery.ui.widget
//= require jquery.ui.mouse
//= require jquery.ui.draggable
//= require twitter/bootstrap
//= require_tree .
$(function() {
$( "#draggable" ).draggable({ handle: "#handle" , containment: [0,0,1200,1000] , cursor: "crosshair" });
});
UPDATE: This was caused by deleting an image , making a new image with the same name, and then pushing to heroku. Don't do that!
There's no need to precompile on your local. Sprockets automatically compiles static asset at server start.
To resolve things, delete the compiled files in the public/assets directory, commit your changes, then deploy to Heroku again.
Then, in Heroku, compile your assets by running the following from your command line:
# from command line via the Heroku Toolbelt
rake run assets:precompile
Your local assets will compile on server start (thus rendering jQuery usable again), and your assets on Heroku will be precompiled to the public/assets directory as they should be.
UPDATE:
You may need to clear your browser cache to ensure that the updated asset path is loaded into your markup.
I'm setting up my Rails app on Heroku, and running into a problem where all my CSS assets are loaded but none of my JS assets are. I'm using memcached, and followed these instructions: https://devcenter.heroku.com/articles/rack-cache-memcached-rails31.
One thing I noticed is that when I change config.assets.compile to "true" in my production.rb file, all the JS is loaded successfully but obviously the initial load of the site is tremendously slow. How can I troubleshoot this problem? I'm new to Heroku, so am unsure and haven't found anything online that is helpful.
I assume that since all my JS files are in /assets/javascripts/, that the should be included automatically, but it seems as if they're not.
Here's my production.rb:
BrainDb::Application.configure do
# Settings specified here will take precedence over those in config/application.rb
client = Dalli::Client.new
config.action_dispatch.rack_cache = {
:metastore => client,
:entitystore => client,
:allow_reload => false
}
# 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 = true
config.static_cache_control = "public, max-age=2592000"
# 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
config.i18n.fallbacks = true
# Send deprecation notices to registered listeners
config.active_support.deprecation = :notify
end
I also removed everything from public/assets so it would precompile. Here's application.rb:
require File.expand_path('../boot', __FILE__)
require 'rails/all'
if defined?(Bundler)
# If you precompile assets before deploying to production, use this line
Bundler.require(*Rails.groups(:assets => %w(development test)))
# If you want your assets lazily compiled in production, use this line
# Bundler.require(:default, :assets, Rails.env)
end
module BrainDb
class Application < Rails::Application
# Configure the default encoding used in templates for Ruby 1.9.
config.encoding = "utf-8"
# Configure sensitive parameters which will be filtered from the log file.
config.filter_parameters += [:password]
# Enable escaping HTML in JSON.
config.active_support.escape_html_entities_in_json = true
config.active_record.whitelist_attributes = true
config.assets.initialize_on_precompile = false
# Enable the asset pipeline
config.assets.enabled = true
config.cache_store = :dalli_store
# Version of your assets, change this if you want to expire all your assets
config.assets.version = '1.0'
end
end
UPDATE
Application.js:
//
//= require jquery
//= require jquery_ujs
Add
//= require_tree . at last and precompile
in your application.js.