Rails 3.1 assets pipeline in production

三世轮回 提交于 2019-11-29 04:34:26
rpechayr

Richard Hulse answers pointed me to the right direction. What happens is really subtle..

The answer to my question is Rails 3.1 assets has no fingerprint in production.

Basically, my project use mongoid instead of ActiveRecord. According to Mongoid documentation about configuration, the application.rb file can be modified to not include ActiveRecord which means removing:

require railties/all

And replacing it with:

require "action_controller/railtie"
require "action_mailer/railtie"
require "active_resource/railtie"
require "rails/test_unit/railtie"
# require "sprockets/railtie" # Uncomment this line for Rails 3.1+

I was so used to doing this manipulation with rails 3.0.x that I did not pay attention to the comment related to Rails 3.1

My problem was that I was not requiring sprockets !

Thank you all for your help !

The reason you can access stylesheet.css in development is because of how Sprockets works.

In development mode ALL requests to anything under /assets are sent to Sprockets to process. Sprockets will directly map requests to paths, one-to-one, so you can access any assets stored in app/assets/etc.

All requests go through Sprockets; it is serving the files to your browser.

In production things are different. A fingerprint is added to filenames, and the expectation is that you'll precompile your assets to static files. This is for performance reasons - Sprockets is not fast enough to serve lots of requests.

Only those CSS and JS files referenced by the default manifests are compiled into application.css and application.js. Other files that you reference are not precompiled unless they are added to the config.assets.precompile array in your config file.

You say that the files resolve to /stylesheets/stylesheet.css. The pipeline should generate a path like this in development: /assets/applicaton.css. In production there should be a fingerprint in the filename. What you have posted suggested that the pipeline is not enabled (these are the old, pre 3.1, locations for the files).

If this is an upgraded app, it is likely that you have missed some crucial config option. This is the main cause of dev->production issues. Check that the pipeline options are set exactly as they are in the last section of the pipeline guide. (My guess is that you are missing config.assets.enabled = true in application.rb)

And for clarity I would suggest changing the name of stylesheet.css to admin.css, while including this in the precompile array (as you already had done).

With the config options set correctly, and your admin manifest included in precompile, you should have application.css available for the front end and admin.css available for the back-end, both linkable via the helper methods.

You application.css should be a manifest file, meaning that your when you run your program it should include your stylesheet.css automatically.

make sure it has these two lines.

application.css:

/*
 *= require_self
 *= require_tree . 
*/

If it does then something else isn't working properly, you shouldn't need the line:

config.assets.precompile += ['stylesheet.css']

If that isn't working either make sure you have all the settings enabled from this Asset Pipeline guide.

While I was looking around actionpack to see what might be causing this problem I found this little gem in the 3.1.1 changelog:

javascript_path and stylesheet_path now refer to /assets if asset pipelining is on.

Soooo, If you're using 3.1.0 upgrade to 3.1.1 and see if it fixes it. If you've already upgraded then were back to square one. But it does seem to describe your problem since stylesheet_link_tag uses stylesheet_path interally.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!