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
- Ruby版本2.2.1
我已经在生产环境中运行我的应用程序,执行以下步骤:
I have managed to run my application in production doing these steps :
-
rake db:create RAILS_ENV = production
-
= production
-
rake assets:precompile
和rake assets:precompile RAILS_ENV =生产
rake db:create RAILS_ENV=production
rake db:migrate RAILS_ENV=production
rake assets:precompile
andrake assets:precompile RAILS_ENV=production
我在我的 application.js
和 application.css
文件,因为所有的require都不在正确的顺序。我不得不在底部放置 require_tree
,在顶部放置 require jquery
,然后放置 require
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
.
现在我的档案如下:
//= require jquery
//= require jquery_ujs
//= require bootstrap
//= require dataTables/jquery.dataTables
//= require dataTables/extras/dataTables.tableTools
//= require_tree .
-
application.css
file: application.css
file :
/*
*= require_self
*= require bootstrap
*= require dataTables/jquery.dataTables
*= require_tree .
*/
-
rb
档案: 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
文件: 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
然而,我写的所有javascript不是在生产中工作。我查看了我的浏览器控制台和我的javascript加载我可以看到它在DOM当我右击检查元素。
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".
所以为什么我的javascript工程
So why my javascript works in development but is not working in production ?
我尝试了几个选项:
- 第一次尝试:我添加行
config.assets.precompile + =%w(* .css * .js)
- 第二次尝试:
config.assets.precompile + =%w(* .css * scss * .js)
- p> 3rd try:我发现在
config / initializers / assets.rb
是一行代码非常类似于我在第一次和第二次尝试,所以我启用config / initializers / assets.rb
中的代码行:Rails.application.config.assets.precompile + =%w(search.js)
但我还没有。
- 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 inconfig/initializers/assets.rb
:Rails.application.config.assets.precompile += %w( search.js )
but I still have nothing.
第四次尝试:我阅读了另一个 post在Stackoverflow上删除在我的GemFile jquery-ui-rails
可以工作,但我还没有javascript
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
这里是我的GemFile:
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]
有人会知道发生了什么吗?为什么我的javascript不是在生产中工作,但在开发中工作良好?
推荐答案
由web服务器(Apache,Nginx)并将工作。直到该修复,您可以加载生产中的所有文件,而不是加载压缩的单个application.js和application.css文件。为了在production.rb文件中禁用配置变量。
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
这篇关于Rails 4 - 在生产中我的javascript加载,但不工作,而它在开发中工作良好的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!