簡體   English   中英

Rails 4 - 在生產中,我的 javascript 已加載,但在開發中運行良好時不起作用

[英]Rails 4 - In production my javascript is loaded but does not work while it works well in development

我與:

  • Rails 版本 4.2.3
  • 紅寶石版本 2.2.1

我已經設法在生產中運行我的應用程序,執行以下步驟:

  1. rake db:create RAILS_ENV=production
  2. rake db:migrate RAILS_ENV=production
  3. rake assets:precompilerake assets:precompile RAILS_ENV=production

我的application.jsapplication.css文件遇到了一些麻煩,因為所有的 require 都沒有按正確的順序排列。 我不得不把require_tree放在底部,然后直接在頂部require jquery ,然后是require jquery_ujs

現在我的文件如下:

  • application.js文件:
//= require jquery
//= require jquery_ujs
//= require bootstrap
//= require dataTables/jquery.dataTables
//= require dataTables/extras/dataTables.tableTools
//= require_tree .
  • application.css文件:
 /*
 *= require_self
 *= require bootstrap
 *= require dataTables/jquery.dataTables
 *= require_tree .
 */
  • production.rb .rb文件:
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文件:
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

然而,我寫的所有 javascript 都沒有在生產中工作。 我查看了我的瀏覽器控制台,我的 javascript 已加載,當我右鍵單擊“檢查元素”時,我可以在 DOM 中看到它。

那么為什么我的 javascript 可以在開發中運行,但在生產中不起作用?

我嘗試了幾種選擇:

  • 第一次嘗試:我添加了行config.assets.precompile += %w( *.css *.js)
  • 第二次嘗試: config.assets.precompile += %w( *.css *scss *.js)
  • 第三次嘗試:我發現config/initializers/assets.rb中的一行代碼與我在第一次和第二次嘗試中嘗試的非常相似,所以我在config/initializers/assets.rb啟用了這行代碼: Rails.application.config.assets.precompile += %w( search.js )但我仍然什么都沒有。

  • 第 4 次嘗試:我在 Stackoverflow 上的另一篇文章中讀到,在我的 GemFile 中刪除jquery-ui-rails可以工作,但我仍然沒有 javascript

這是我的 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]

有人會知道發生了什么嗎? 為什么我的 javascript 不能在生產中運行,但在開發中運行良好?

在生產模式下,它應該由 Web 服務器(Apache、Nginx)提供服務並且可以工作。 在修復之前,您可以加載生產中的所有文件,而不是加載壓縮的單個 application.js 和 application.css 文件。 為此,在 production.rb 文件中禁用配置變量。 像下面

config.serve_static_assets = false

問題出在您的 application.js 中,更具體地說,我相信您的引導程序被調用的位置。 對我來說修復它的是將引導程序移到底部。

資產管道在 dev 中被禁用,這就是為什么您在那里沒有遇到相同的問題。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM