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

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

Related

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.

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

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

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.

Ruby on Rails 3.1 assets:precompile and images

I can't get the Rails 3.1 asset pipeline precompilation to work in production mode. It always fails on images referenced within SCSS with an error like:
$ bundle exec rake assets:precompile RAILS_ENV=production
rake aborted!
rails.png isn't precompiled
(in /home/florian/AssetTest/app/assets/stylesheets/application.css.scss)
But when I look in the public/assets directory, the image is there, so it is precompiled:
$ ls public/assets | grep rails
rails-dd352fc2630e5f9aa5685ef1d7fe5997.png
The SCSS file in this case just contains some test code:
/*
* This is a manifest file that'll automatically include all the stylesheets available in this directory
* and any sub-directories. You're free to add application-wide styles to this file and they'll appear at
* the top of the compiled file, but it's generally better to create a new file per style scope.
*= require_self
*= require_tree .
*/
body {
background: #ffffff image-url('rails.png') no-repeat 0 center;
}
If I don't use the image-url helper in SCSS, but just url('/assets/rails.png'), precompilation works fine, and a manifest.yml file is generated in public/assets.
The interesting thing is: if I change the SCSS back to image-url('rails.png') and run another precompilation, it still works (I guess because the image is now already listed in the manifest file).
What am I doing wrong here? I don't really want to disregard the helper methods (as using them is The Way You Should Do It, right?), and I definitely don't want to create the manifest file manually...
I've run into the same problem myself. This is apparently a bug in Rails 3.1.0, and will hopefully be fixed in short order...
In any event, in production.rb, you can try this:
config.assets.compile = true
You likely have it set to false, which it should be. However, having it set to false causes issues when you use asset helpers in SCSS as you're trying to do. Setting that value to true seems to properly allow compilation while using those helpers.
Take a look at this issue on github for some details.

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