Rails: 生产环境中Javascript无法工作

5

目前为止,这是我所做的。

  1. 我在Capfile中加载了资源:js仍然无法工作 http://guides.rubyonrails.org/asset_pipeline.html#precompiling-assets

  2. 我将coffee-rails从资产移动到主要部分:仍然无法工作

    gem 'coffee-rails','~> 3.2.1'

    group :assets do gem 'sass-rails','~> 3.2.3' end

更新:生产环境

SolidAdmin :: Application.configure do #在此处指定的设置将优先于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
  Paperclip.options[:command_path] = "/usr/bin/"
  # 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

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

其他的基本上都是默认/注释掉的


我正在從外部服務器加載所有js。例如,我從谷歌加載jquery,從另一個服務器加載另一個腳本,但這兩者都未在生產環境中顯示。它們在開發環境中都有顯示。我會更新我的帖子。 - mystic cola
你们的生产环境设置是什么样子的? - Tyler
好的,我马上发布。 - mystic cola
已更新至生产环境。 - mystic cola
也许如果您发布生产环境的链接,我们可以进行测试并检查发生了什么? - Juan Guerrero
显示剩余2条评论
4个回答

5

修复了!已切换:

config.assets.compile = false

to:

config.assets.compile = true

现在它可以正常工作了。

2
这不是最好的方式,因为如果你不小心的话,就可能需要在生产环境重新编译资源。最好在部署到生产环境之前使用 rake assets:precompile - Juan Guerrero
嗯,Juan,我试过了……什么都没发生。我试过了,我试过上面的所有东西……这是一个非常奇怪的问题。我不明白为什么会这样。 - mystic cola
1
@JuanGuerrero 我在本地运行了 rake assets:precompile 命令,并将生成的文件推送到生产环境。这对我来说是有效的。 - mhz

5

编辑:在聊天中交谈后,发现页面中包含两个jquery的问题。

检查了您的页面(liquid-radio.com)后,我认为存在一个严重的错误:

<!DOCTYPE html>
<html>
<head>
  <title>liquid.radio</title>
  <link href="/assets/application-c9ed21e2be2e7bb9955d6a0d89357d16.css" media="all" rel="stylesheet" type="text/css" />
  <script src="/assets/application-09500810259983928e0b1b4d46b49071.js" type="text/javascript"></script>
  <meta content="authenticity_token" name="csrf-param" />
<meta content="MYdcXuDSzYg1qSHrRwx0y0VK5VmqhWmLLGiYSOX7pOI=" name="csrf-token" />
</head>
<body style="background: #FFF;">

<!DOCTYPE html>
<html>
    <head>

你第一部分重复了几次,导致你的html无效。

我其实并没有...那是application.html.erb...(昨晚我才发现它的存在)。嗯...看起来我得把我的页眉和页脚复制粘贴到那里(这样反正也能省去一些麻烦)。等我试过之后再回来汇报结果。 - mystic cola
不行!我更新了application.html.erb文件,并包含了我一直放在页面上的所有信息...但它仍然无法加载javascript。事实上...如果你现在去页面上看(我可能很快会改变它),我甚至在application.html.erb上调用了其中一个脚本,但它仍然无法工作... - mystic cola
你介意分享application.html.erb和根视图代码吗?如果这是开源的,甚至更好的话,可以提供一个链接到repo。 - Juan Guerrero
@mysticcola 再次检查您的页面源代码,我发现您将jquery包含了两次(一个在application.js中,可能是通过jquery-rails gem实现的,另一个来自google cdn)。此外,您有2个body开放标签。是否可以私下联系您(skype或其他方式),因为这不再是关于javascript的问题了? - Juan Guerrero
实际上,StackOverflow内置了一个聊天功能...但我不知道如何使用它。:) 我曾经用过一次。但如果你有Yahoo Messenger的话,可以联系我。我的用户名是mysticcola。 - mystic cola
让我们在聊天中继续这个讨论:http://chat.stackoverflow.com/rooms/33964/discussion-between-mystic-cola-and-juan-guerrero - mystic cola

4

我使用的是rails 4.2.6版本。

为了解决我的错误,我需要运行以下步骤:

  1. rake assets:clean - 清理我的assets
  2. 我需要修复application.js文件中的任何问题。这主要是我导入外部js库的顺序问题。
  3. rake assets:precompile - 预编译assets文件
  4. config/environments/production.rb中,我需要更改以下这行代码

    config.serve_static_files = ENV['RAILS_SERVE_STATIC_FILES'].present?

    config.serve_static_files = true

这意味着rails应用程序现在可以提供由assets:precompile生成的静态文件。

在执行此操作并重新启动应用程序后,一切都正常了!


1
这样做的一个问题是你现在的Rails服务器(puma、unicorn等)将提供静态文件。在生产服务器上,你需要让例如Nginx这样的服务器提供这些静态页面。
如果你的JS/CSS在开发中有效,则只需要在推送到生产环境之前运行rake assets:precompile。然后就是配置你的应用服务器与生产服务器一起工作的问题。

谢谢。在本地运行rake assets:precompile并将更改推送到生产环境中已经成功。 - bonafernando

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接