Asset pipeline looks in wrong directory - ruby-on-rails

I added this line in my layout file:
<%= stylesheet_link_tag "bootstrap", "bootstrap-responsive", :cache => true%>
Which produces this error message:
Errno::ENOENT in Test#index
No such file or directory - Asset file not found at '/Developer/Workspace/MyProj/public/stylesheets/Developer/Workspace/MyProj/app/assets/stylesheets/bootstrap.css'
It appears to have concatenated the path to the folder twice before looking for the file. Is this a known issue with rails 3.2.3? Or is there some setting I mucked up?

try removing ":cache => true"
If you take a look at https://github.com/rails/rails/pull/6752/files#L0R40 you will see the line, which produces wrong path when cache or concat options are enabled. In this case paths will looks like "/Absolute/path/to/public/" + "/Absolute/path/to/asset/file.ext" which is wrong.
-- Rails pull request that seems to be related to your issue.

Related

Rails PDFKit - Errno::ENOENT (No such file or directory) when using to_file

Whenever I try to generate a pdf using to_file, the process will just hang, and when I stop the development server I get Errno::ENOENT (No such file or directory - path/to/pdf). However, I am able to render a pdf inline using to_pdf. I'm also able to generate PDFs from the command line in the same folder that I'm trying to generate them in with to_file.
I'm using Rails 3.2.12 and pdfkit 0.8.2. I've tried using wkhtmltopdf versions 0.9.6 through 0.12.4. I'm on Ubuntu 14.04.
Example from controller:
html = render_to_string(:action => "show.html.erb", :formats => :html)
kit.stylesheets << "{Rails.root}/app/assets/stylesheets/stylesheet1.css"
kit.stylesheets << "#{Rails.root}/vendor/assets/stylesheets/stylesheet2.css"
kit.to_file("#{Rails.root}/folder_to_write_to/generated_pdf.pdf")
Turns out the issue was the asset pipeline conflicting with wkhtmltopdf. Added config.threadsafe! to development.rb and it started working.
Another issue can be the default options passed. For example, when I left the default print_media_type option in place, found this message in the log:
The switch --print-media-type, is not support using unpatched qt, and will be ignored."
Only when I override that does it work for me, either in the initializer or like so:
PDFKit.new(html, {print_media_type: false})
The message says it'll be ignored, but it wasn't. It was causing the file to not get generated.

Manually compiling Sass in Rails 4.2

I'm manually compiling Sass in a Rails 4.2 app, and I'm running into issue with Sass' #import. It's giving me the following error - "File to import not found or unreadable..." Here's a code snippet.
scheme_css_dir = "#{Rails.root}/app/assets/schemes/#{scheme}/css"
template = File.read("#{scheme_css_dir}/styles.css.scss")
engine = Sass::Engine.new(template, {
:syntax => :scss,
:cache => false,
:read_cache => false,
:style => :compressed,
:load_paths => [scheme_css_dir]
})
output = engine.render
styles.css.scss contains several #import statements, and it's failing on the first one. Here's what the first one looks like.
#import "./flexslider";
There is a flexslider.css.scss file in the same directory as style.css.scss. I'm supplying that directory in the :load_paths option. Interestingly enough, if I change the #import line to...
#import "./flexslider.css.scss";
...then it gets to the next #import line in the file - where it throws the same error. This style.css.scss manifest complies successfully during deploy as is (without adding extensions to all the #imports).
What am I missing? Is there additional config that I need in order to use Sass::Engine to compile css manifest just like Rails would during deploy?
FYI - I'm using the latest version of sass-rails (5.0.3).
Any help is much appreciated. Thanks!
UPDATE: Thanks to papirtiger's comment, I was able to resolve the issue with #import by updated the extensions of my files from .css.scss to .scss. However, I'm still running into errors with asset helpers - specifically image-url and font-url. Here's the error I'm seeing...
undefined method `[]' for nil:NilClass
The error occurs in a sprockets_context method in sprockets (3.1.0) lib/sprockets/sass_processor.rb
def sprockets_context
options[:sprockets][:context]
end
Any thoughts on how to get pass this hurdle?

Attach a pdf in asset pipeline using ActionMailer Rails 4

I'm trying to attach a file to an email. The file is in assets/downloads/product.pdf
In the mailer, I have:
attachments["product.pdf"] = File.read(ActionController::Base.helpers.asset_path("product.pdf"))
I've tried:
attachments["product.pdf"] = File.read(ActionController::Base.helpers.asset_url("product.pdf"))
...and even:
attachments["product.pdf"] = File.read(ActionController::Base.helpers.compute_asset_host("product.pdf") + ActionController::Base.helpers.compute_asset_path("product.pdf"))
I always get the same error:
EmailJob crashed!
Errno::ENOENT: No such file or directory - //localhost:3000/assets/product.pdf
...or a variation on the theme. But even when I try using asset_url in the view or just put the url in the browser it works:
http://localhost:3000/assets/product.pdf
I've also tried using straight up:
File.read("app/assets/downloads/product.pdf")
File.read("downloads/product.pdf")
...which works in dev environment but not on staging server (heroku). Error is still:
Errno::ENOENT: No such file or directory - downloads/product-market-fit-storyboard.pdf
Also tried:
File.read("/downloads/product.pdf")
File.read("http://lvh.me:3000/assets/product.pdf")
...don't work at all.
Ideas?
you should use syntex like this.it is work for me may be will work for you also.
File.open(Dir.glob("#{Rails.root}/app/assets/downloads/product.pdf"), "r")
When using mailer, you shouldn't use assets pipline. Asset pipeline would be useful if you wanted to have link to a file inside your email. When rendering an email, action mailer has access to files in app directory.
Please read about attachments in action mailer guide. As you can see, you just need to pass path to a file, not url:
attachments['filename.jpg'] = File.read('/path/to/filename.jpg')

Rails how to serve .pde asset files

I have in my .html file:
<%= javascript_include_tag "processing-1.4.1.min" %>
<canvas data-processing-sources="/assets/pjs/my.pde"></canvas>
and the asset lies exactly there: app/assets/pjs/my.pde.
I get this error in the server:
Served asset /pjs/my.pde - 404 Not Found (10ms)
and this error in the javascript:
Uncaught Processing.js: Unable to load pjs sketch files: /assets/pjs/my.pde ==> Invalid XHR status 404
My application.rb says:
config.assets.enabled = true
Might be a really stupid mistake but i just don't get it. I'd really appreciate if anyone can tell me how to solve this.
use the erb extension for your view file, then use asset_path 'my.pde'. When using the asset pipeline you can't link directly to a path because files will have fingerprints added to them.

Dynamic CSS using Sprockets

I'm working on a site builder in rails and I would like to render the sites css using Sprockets SCSS processors. Since the user can change colors and logos, I can't use Sprockets precompilation so I've started working on a Rails SCSS template handler to handle dynamic views.
The goal is to compile 'app/views/sites/show.css.scss' any time /sites/43543.css is requested. Here's what I have so far. You'll notice I first run the template through the ERB processor and then attempt to run it through Sprockets.
https://gist.github.com/3870095
Manuel Meurer came up with an alternative solution that writes the ERB output to a path and then triggers the Asset Pipeline to compile it. I was able to get his solution to work locally but it wont work on heroku because the asset path is not writable. Files can only be written to the tmp directory and those files are only guaranteed for a single request.
http://www.krautcomputing.com/blog/2012/03/27/how-to-compile-custom-sass-stylesheets-dynamically-during-runtime/
After a long day I was able to solve my problem thanks to John Feminella and his post on google. The challenging part for me was figuring out how to create a new Sprockets::Context. Luckily John's solution doesn't require a Context.
Updated gist here
Attempt #1
This code does not allow importing css files from the asset pipeline.
#import "foundation"; works because foundation is loaded as a compass module
#import "custom_css"; results in an error message saying the file could not be found
def call(template)
erb = ActionView::Template.registered_template_handler(:erb).call(template)
%{
options = Compass.configuration.to_sass_engine_options.merge(
:syntax => :scss,
:custom => {:resolver => ::Sass::Rails::Resolver.new(CompassRails.context)},
)
Sass::Engine.new((begin;#{erb};end), options).render
}
end
Attempt #2
This code fails to embed base64 urls using asset-data-url
def call(template)
erb = ActionView::Template.registered_template_handler(:erb).call(template)
%{
compiler = Compass::Compiler.new *Compass.configuration.to_compiler_arguments
options = compiler.options.merge({
:syntax => :scss,
:custom => {:resolver => ::Sass::Rails::Resolver.new(CompassRails.context)},
})
Sass::Engine.new((begin;#{erb};end), options).render
}
end
Attempt 3
Turns out you can use empty values while creating the context. Code below works in development but throws an error in production.
ActionView::Template::Error (can't modify immutable index)
It appears the error occurs in Sprockets::Index which is used instead of Sprockets::Environment in production. Switching to Sprockets::Environment doesn't solve the problem either.
def call(template)
erb = ActionView::Template.registered_template_handler(:erb).call(template)
%{
context = CompassRails.context.new(::Rails.application.assets, '', Pathname.new(''))
resolver = ::Sass::Rails::Resolver.new(context)
compiler = Compass::Compiler.new *Compass.configuration.to_compiler_arguments
options = compiler.options.merge({
:syntax => :scss,
:custom => {:resolver => resolver}
})
Sass::Engine.new((begin;#{erb};end), options).render
}
end

Resources