Should "config.assets.compress = true" in development create bad css links? - asset-pipeline

In Rails 3.2.3, the html head tag contains links to all the styleheets in app/assets/stylesheets even though only the stylesheet_link_tag only referenced 'application' and all of the links are broken. What's wrong here:
When config/environments/development.rb has
config.assets.compress = true
and app/assets/stylesheets/application.rb contains
*= require_tree .
app/views/layouts/application.rb stylesheet section in the head tag contains only
<%= stylesheet_link_tag( 'application' ) %>
then there is one link tag for every css file in app/assets/stylesheets. Each of the links is 404 except for the application.css and that one has all of the content that should be at each of the respective links.
Is this a Rails bug? Is it wrong to want to test compression in dev? Am I doing something wrong?

Not sure if this will help, but in my development.rb i have this:
config.assets.compress = false
config.assets.debug = false
config.assets.compile = true
These settings seem to work for me when i precompile in development. Also in application.rb i require all my assets as some of them don't work with just having the *= require_tree.
My application.css
*= require_self
*= require 'blueprint/screen'
*= require jquery.jgrowl
*= require jquery-ui-1.8.18.custom.css
*= require 'custom'
I had to leave out the *= require_tree bit, because it was causing unexpected issues. Restart server. Hope this provides some direction

Related

Rails 4.1 assets pipeline production, background image path different in two seperate css files after precompile

I have a rails 4.1.1 application in production, in that i have a fancybox.css file from the gem 'fancybox2-rails', '0.2.8'
In the application.css i have added the file as below.
*= require jquery.ui.datepicker
*= require jquery.ui.slider
*= require fancybox
*= require_tree .
*= require_self
Also in another lockbox.css file i have added it as below
*= require jquery.ui.datepicker
*= require jquery.ui.slider
*= require fancybox
In production.rb I have added the configuration for additional compiling of the lockbox.css file like this
config.assets.precompile += %w( lockbox.css )
My production environment is configured as a subdirectory
www.test.com/v1
www.test.com/v2
The problem is in the lockbox.css file , where the background url path of an background image is different in application.css and lockbox.css
eg in application.css
the path is correct, v1/assets/my_image.png
but in lockbox.css
the same path is compiled wrongly as, v1/my_image.png
Please explain why this is happening and what can be done to solve this issue.
put this configuration in your production.rb
config.action_controller.relative_url_root = '/your-sub-uri'
This will add the sub-uri to the 3rd party gem assets.
i got this configuration from here

Rails 3.2 Assets in Production

I'm at my wits end with this. It seems all of the newer rails apps I make its set a couple of configs in environments/production.rb, deploy and move on with my life. But now we're migrating a few rails apps to a new server and it seems all of them have this issue when deploying to production.
What appears to be happening is that neither my javascripts or stylesheets are getting compiled. And I see none of the styles for the app and the javascript does not work.
config/application.rb:
require File.expand_path('../boot', __FILE__)
require 'rails/all'
if defined?(Bundler)
Bundler.require(:default, :assets, Rails.env)
end
module MyApp
class Application < Rails::Application
... omitted code ...
# Enable the asset pipeline
config.assets.enabled = true
# Version of your assets, change this if you want to expire all your assets
config.assets.version = '1.0'
config.generators.stylesheet_engine = :scss
end
end
config/environments/production.rb:
MyApp::Application.configure do
# 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
# 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 = true
# Generate digests for assets URLs
config.assets.digest = true
... omitted code ...
end
In my application-<...>.js:
// This is a manifest file that'll be compiled into including all the files listed below.
// Add new JavaScript/Coffee code in separate files in this directory and they'll automatically
// be included in the compiled file accessible from http://example.com/assets/application.js
// It's not advisable to add code directly here, but if you do, it'll appear at the bottom of the
// the compiled file.
//
//= require jquery
//= require jquery_ujs
//= require_tree
;
And my application-<...>.css is completely empty. What am I missing?
So after a long half day yesterday and about an hour today, I suddenly realized something... I ran into this before.
Sure enough... Updating rails to 3.2.14 and deploying again fixed the whole thing.

Rails asset pipeline not working in production environment?

I have recently upgraded an app from Rails 3.0 to 3.1. I have followed any instructions I could find for enabling the asset pipeline but it always fails when in the production environment:
<%= javascript_include_tag "application" %>
gives me
<script src="/javascripts/application.js" type="text/javascript"></script>
which is missing a digest and I get the following error:
cache: [GET /javascripts/application.js] miss
Started GET "/javascripts/application.js" for 127.0.0.1 at 2011-10-03 23:31:36 +0100
ActionController::RoutingError (No route matches [GET] "/javascripts/application.js"):
I've tried variations of these settings in application.rb:
require File.expand_path('../boot', __FILE__)
#require 'rails/all'
require "action_controller/railtie"
require "action_mailer/railtie"
require "active_resource/railtie"
require "rails/test_unit/railtie"
if defined?(Bundler)
# If you precompile assets before deploying to production, use this line
Bundler.require *Rails.groups(:assets => %w(development test))
# If you want your assets lazily compiled in production, use this line
# Bundler.require(:default, :assets, Rails.env)
end
module Blog
class Application < Rails::Application
config.autoload_paths += %W(#{config.root}/lib)
config.encoding = "utf-8"
config.filter_parameters += [:password]
config.assets.enabled = true
config.assets.version = '1.0'
end
end
and full production.rb (minus some comments)
Blog::Application.configure do
config.cache_classes = true
config.consider_all_requests_local = false
config.action_controller.perform_caching = true
config.serve_static_assets = false
config.assets.compress = true
config.assets.compile = false
config.assets.digest = true
config.i18n.fallbacks = true
config.active_support.deprecation = :notify
end
I have ran the rake assets:precompile task.
Am I missing any obvious steps?
Edit: Some additional details:
My assets are in app/assets folder. app/assets/images, app/assets/javascripts, app/assets/stylesheets, etc.
I see my files generated in my public/assets directory with names and digests.
app/assets/javascripts/application.js does indeed compile to something like public/assets/application-6ec417a53cb2bdb949966a153a61e7b1.js They end up in the public directory.
Sprockets is not getting loaded.
In an effort to remove active record in a previous version of rails (a la this question Remove ActiveRecord in Rails 3 (beta)) the require 'rails/all' was replaced by
require "action_controller/railtie"
require "action_mailer/railtie"
require "active_resource/railtie"
require "rails/test_unit/railtie"
What was missing here was sprockets/railtie
See the Upgrading to Rails 3.1 Railscast
Make sure your assets are in app/assets folder. app/assets/images, app/assets/javascripts, app/assets/stylesheets, etc.
Execute rake assets:precompile
You should see files generated in your app/public/assets directory with names and digests if enabled.
app/assets/javascripts/application.js would compile to /assets/application-6ec417a53cb2bdb949966a153a61e7b1.js
If the asset is named similar to above with a digest, Production.rb should have the following config:
# Generate digests for assets URLs
config.assets.digest = true
If you look at the web page source you should see something similar to the following:
<script src="/assets/application-6ec417a53cb2bdb949966a153a61e7b1.js" type="text/javascript"></script>
Try to manually load the file by going to http://example.com//assets/application-6ec417a53cb2bdb949966a153a61e7b1.js
The file should load, if not try checking permissions and further logs.

wicked_pdf + rails' asset pipeline + sass import in production

I'm successfully using wicked_pdf with SASS in development. I'm including a single .scss file, which contains several import rules for other .sass and .scss files, via this helper:
def wicked_pdf_stylesheet_link_tag(*sources)
sources.collect { |source|
"<style type='text/css'>#{Rails.application.assets.find_asset("#{source}.css").body}</style>"
}.join("\n").gsub(/url\(['"](.+)['"]\)(.+)/,%[url("#{wicked_pdf_image_location("\\1")}")\\2]).html_safe
end
But switching to production the app still looks for the imported files which aren’t found.
I've added then a second manifest file to be pre–compiled in production.rb (config.assets.precompile += %w(pdf.css)) which contains a single require rule to pick up the mentioned .scss file. This file is compiled just fine but it seems that the helper doesn't pick up the right file in production and still looks to load the imported .sass files.
Has anyone experience how to solve this? The PDF creation requires absolute paths, which makes this task a bit more difficult.
I have wicked pdf working in development and production. This is the core of my wicked_pdf config:
I've updated WickedPdfHelper (loaded from initializers/wicked_pdf.rb) based on a wicked_pdf pull request from github user antti
module WickedPdfHelper
def wicked_pdf_stylesheet_link_tag(*sources)
sources.collect { |source|
"<style type='text/css'>#{Rails.application.assets.find_asset(source+".css")}</style>"
}.join("\n").html_safe
end
def wicked_pdf_image_tag(img, options={})
asset = Rails.application.assets.find_asset(img)
image_tag "file://#{asset.pathname.to_s}", options
end
def wicked_pdf_javascript_src_tag(jsfile, options={})
asset = Rails.application.assets.find_asset(jsfile)
javascript_include_tag "file://#{asset.pathname.to_s}", options
end
def wicked_pdf_javascript_include_tag(*sources)
sources.collect{ |source| "<script type='text/javascript'>#{Rails.application.assets.find_asset(source+".js")}</script>" }.join("\n").html_safe
end
end
then in app/assets/stylesheets/pdf.css I require a few sass stylesheets:
/* ...
*= require ./_colors
*= require_directory ./pdf
*= require_self
*/
(remember that if you're modifying initializers or anything in config/, you'll need to re-start your rails app to pull in the changes)
I wrote a article on this at: http://anlek.com/2011/09/wicked_pdf-working-in-rails-3-1/
It's very similar to Philip's solution with a few modifications.

How to install 'Ckeditor' for 'Active Admin' with 'Rails 3.1'

I looking for how to install Ckeditor for Active_Admin
I already installed Ckeditor and I have put this line in my 'initializers/active_admin.rb' file.
# To load a javascript file:
config.register_javascript
'ckeditor/config.js'
And the line
config.autoload_paths += %W(#{config.root}/app/models/ckeditor)
in my 'config/application.rb'
In the begining the 'config.js' file wasn't loaded because when I install Ckeditor gem it creates a folder named Ckeditor in public/javascripts but with rails 3.1, I had to move this folder into assets/javascripts...
That's what I've done.
Now the file "config.js' is loaded.
But now I don't know what I have to do next
Rails 3.2
activeadmin-0.4.3
in 'initializers/active_admin.rb' file.
config.register_javascript 'ckeditor/ckeditor.js'
config.register_javascript 'ckeditor/init.js'
in form
= f.cktext_area :text
If your're using the assets pipeline with external host server you have to hack a little bit.
Your assets/javascripts/active_admin.js should look like this:
//= require active_admin/base
//= require_self
//= require ckeditor/ckeditor
//= require ckeditor/init
var CKEDITOR_BASEPATH = 'http://assets.yourdomain.com/assets/ckeditor/';
Then you're good to go.
Add
var CKEDITOR_BASEPATH = '/assets/ckeditor/';
to the start of your ckeditor.js file.
I believe you are having an issue with the new location of the files in rails 3.1. Make sure all your ckeditor files are located in the javascript/ckeditor folder (or modify the link accordingly).
See http://docs.cksource.com/CKEditor_3.x/Developers_Guide/Specifying_the_Editor_Path
You can also use in your forms
f.input :name, :as => :ckeditor
There is a page in the oficial wiki for this task.

Resources