简体   繁体   English

Rails 4 App-Heroku / Cloudfront-资产无法从原始位置正确提供

[英]Rails 4 App - Heroku / Cloudfront - Assets not serving from origin properly

I have a rails 4 application in which we are using cloudfront as the CDN. 我有一个Rails 4应用程序,我们在其中使用Cloudfront作为CDN。

In the production.rb file, we have the following code: production.rb文件中,我们具有以下代码:

config.action_controller.asset_host = "example3579.cloudfront.net"

We have done some pretty thorough asset separation. 我们已经完成了一些相当彻底的资产分离。 This may not be advisable from an asset pipeline standpoint, but it should make debugging this issue pretty easy. 从资产管道的角度来看,这可能不是明智的选择,但它应该使调试此问题变得非常容易。

The particular page I'm working on is the users#edit view, but the problem is universal. 我正在处理的特定页面是users#edit视图,但是问题很普遍。

The users#edit action uses it's own layout: views/layouts/user_edit.html.erb users#edit操作使用其自己的布局: views/layouts/user_edit.html.erb

The views/layouts/user_edit.html.erb file calls this javascript. views/layouts/user_edit.html.erb文件称为此javascript。

<%= javascript_include_tag 'user_edit'%>

assets/javascripts/user_edit.js 资产/ Java脚本/ user_edit.js

//= require jquery
//= require jquery_ujs
//= require jquery.validate.min
//= require ./webarch/form_validations

On the localhost, this works. 在本地主机上,这可行。 I can access the js in the browser like this: 我可以这样在浏览器中访问js:

http://localhost:3000/assets/user_edit.js

But on production with heroku, the site looks for this file: 但是在使用heroku进行生产时,该站点将查找以下文件:

<script src="http://example3579.cloudfront.net/javascripts/user_edit.js"></script>

The file doesn't work. 该文件不起作用。 I can't access in the browser and the javascript does not render on the page. 我无法在浏览器中访问,并且javascript不在页面上呈现。 I have precompiled the assets. 我已经预编译了资产。

Add gem 'rails_12factor', group: :production # Helpful for rails 4 assets on heroku 添加gem 'rails_12factor', group: :production #有助于heroku上的rails 4资产

And in /config/environments/production.rb 并在/config/environments/production.rb中

config.assets.compile = true

This should work 这应该工作

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM