问题
I was receiving the following error message after precompiling my assets locally and then pushing the code to Heroku:
2012-03-28T17:06:01+00:00 app[web.1]: Started GET "/admin/login" for 67.163.67.203 at 2012-03-28 17:06:01 +0000
2012-03-28T17:06:01+00:00 app[web.1]:
2012-03-28T17:06:01+00:00 app[web.1]: ActionView::Template::Error (File to import not found or unreadable: active_admin/mixins.
2012-03-28T17:06:01+00:00 app[web.1]: Load paths:
2012-03-28T17:06:01+00:00 app[web.1]: /app
2012-03-28T17:06:01+00:00 app[web.1]: /app/vendor/bundle/ruby/1.9.1/gems/activeadmin-0.4.3/app/assets/stylesheets
2012-03-28T17:06:01+00:00 app[web.1]: (in /app/vendor/assets/stylesheets/active_admin.css.scss)):
2012-03-28T17:06:01+00:00 app[web.1]: 7:
2012-03-28T17:06:01+00:00 app[web.1]: 6: <title><%= [@page_title, active_admin_application.site_title].compact.join(" | ") %></title>
2012-03-28T17:06:01+00:00 app[web.1]: 8: <% ActiveAdmin.application.stylesheets.each do |style| %>
2012-03-28T17:06:01+00:00 app[web.1]: 9: <%= stylesheet_link_tag style.path, style.options %>
2012-03-28T17:06:01+00:00 app[web.1]: 10: <% end %>
2012-03-28T17:06:01+00:00 app[web.1]: 11: <% ActiveAdmin.application.javascripts.each do |path| %>
2012-03-28T17:06:01+00:00 app[web.1]: 12: <%= javascript_include_tag path %>
2012-03-28T17:06:01+00:00 app[web.1]: vendor/assets/stylesheets/active_admin.css.scss:2
...
I tried a bunch of different options including the following tip that I found here - http://mrdanadams.com/2011/exclude-active-admin-js-css-rails/ and others on the GitHub page.
None of those options worked. Ultimately, I received some advice to delete my public/assets directory from git, push the code to Heroku and let Heroku precompile the assets. This approach partially worked. I no longer receive the error message when I try to go to the /admin page of my site. However, the active_admin CSS files are missing. It suspects that since Heroku did the precompiling it is not throwing up an error even though the active_admin.css files are not being precompiled. How can I get active_admin.css precompiled?
Btw, I am running rails 3.2.
EDIT:
It appears that my "fix" was not complete. If I have require_tree . or require_directory . in my application.css then it works but it screws up my main apps CSS because they all get compiled to application.css. If I don't have one of those entries then it breaks. Any thoughts on how I can resolve this?
回答1:
I was finally able to resolve this issue. In case someone else runs into this problem, I thought I would document the steps that I took to resolve it.
I ran my application locally in production mode (RAILS_ENV=production rails s
) and was able to duplicate the error that I received on Heroku on my local machine.
I copied my active_admin.css.scss and active_admin.js to the /vendor/assets directory. Since y app
kept telling me that it was missing active_admin/mixins I also copied the entire active_admin directory in assets to the vendor/assets directory. I am not sure if this is necessary or not.
From a Heroku perspective, I've been told, but can not confirm, that production.rb is not read during the precompile so all settings need to be defined in application.rb. So, I made sure that I had the following settings in application.rb -
#Added to fix devise/active admin issue ?
config.assets.initialize_on_precompile = false
# Precompile additional assets. Defaults to [application.js, application.css, non-JS/CSS]
config.assets.precompile += ['active_admin.css.scss', 'active_admin.js']
I found most of above tips around the net (on stackoverflow, heroku, github, etc.). The part that I did not see was the need to make change Bundler.require in application.rb from:
Bundler.require(*Rails.groups(:assets => %w(development test)))
to:
Bundler.require(:default, :assets, Rails.env)
Once I made those changes then I could push the code onto Heroku and let it precompile the assets for me. I hope that this can help someone save some time in resolving this issue.
回答2:
You're on the right track, in letting Heroku compile assets for you. That makes things easiest.
To include additional files in the precompiler manifest, use something like this in your application config (either config/application.rb
or config/environments/production.rb
):
# Precompile additional assets. Defaults to [application.js, application.css, non-JS/CSS]
config.assets.precompile += ['active_admin.css', 'active_admin/print.css', 'active_admin.js']
回答3:
The ActiveAdmin github wiki addresses this issue specifically: https://github.com/gregbell/active_admin/wiki/Heroku-Cedar-deployment-with-the-Asset-Pipeline
Here's what it says at the time of writing:
Try adding the AA assets to the precompile list in
application.rb
(NOTE: you CANNOT add them inproduction.rb
, Heroku does NOT readproduction.rb
during precompile!)# config/application.rb config.assets.precompile += %w( active_admin.css active_admin.js active_admin/print.css )
Try placing
active_admin.css.scss
andactive_admin.js
invendor/assets
instead ofapp/assets
. This prevents inadvertent inclusion of AA assets when you use the sprockets directiverequire_tree .
This is the default sprockets directive inapplication.css
for a new rails app and is why many people get confused about AA assets being required on all parts of their site. Putting the AA assets invendor/assets
prevents this problem but you could just as well put it in a subdirectory ofapp/assets
and avoid using therequire_tree
directive (opting forrequire_directory
instead).Make sure
sass-rails
is available when precompiling. This entails making sure that either the assets group is required during precompile or makingsass-rails
available in all gem groups. Often upgrades from older versions of Rails will not have the correct Bundler require statement so this is important to check if you didn't start your project on Rails 3.1+. If you can runbundle exec rake assets:precompile RAILS_ENV=production
on your machine without errors and with a fake production db configured then you're good.Set up heroku-specific config as directed in their FAQ about deploying using the asset pipeline:
# config/application.rb - NOT production.rb config.assets.initialize_on_precompile = false
回答4:
Had the same issue. Fixed by adding the following to production.rb to precompile additional assets:
config.assets.precompile += %w( active_admin.css active_admin.js)
回答5:
Rather than keeping a list of files, I fixed the problem by changing the flag in production.rb
:
config.assets.compile = true
回答6:
I found that I could resolve this by updating my gemfile - moving the 'sass-rails' gem out of the :assets group and into either the production or general sections.
Apparently this is an issue where Heroku needs the saas-rails gem or it breaks something, so says: http://ygamretuta.me/2011/10/02/setting-up-active-admin-on-heroku-with-rails-3-1-and-cedar/
来源:https://stackoverflow.com/questions/9933514/activeadmin-heroku-stylesheet-config-issue-with-partial-fix