rails 4.1 add videos folder to asset pipeline - ruby-on-rails

Here are my paths so far:
assets
>fonts
>images
>stylesheets
>javascripts
>videos
I would like to bucket videos under the videos folder, but I can not seem to get them to be show up. My current assets.rb initializer looks like:
Rails.application.config.assets.precompile += %w( layout-home.css layout-error.css layout-static-page.css layout-brand.css )
Rails.application.config.assets.precompile += %w( layout-home.js layout-static-page.js layout-brand.js )
Rails.application.config.assets.precompile += [ Dir.glob("#{Rails.root}/app/assets/videos/**/") ]
Any ideas on how to adjust this? No matter how I adjust that last line, I can not get them to pull in.

Add the following to config/initializers/assets.rb
Rails.application.config.assets.paths << "#{Rails.root}/app/assets/videos"
Do not forget to restart the server after changing an initializer.

Related

Rails and SLIM html image_tag points to images instead of assets

I have a scenario where I need to send emails using a template written in SLIM. I fixed my environment/development.rb with the following config that I find necessary (also based from my research)
config.assets.precompile += %w( print.css )
config.serve_static_assets = true (still using Rails 4.1.16)
config.action_mailer.default_url_options = { host: 'http://localhost:3000' }
config.action_controller.asset_host = 'http://localhost:3000'
config.action_mailer.asset_host = config.action_controller.asset_host
I know it's pointing correctly to my root url on this step. Then In my mailer.html.slim I have something like this:
div align="left" style="border-collapse: collapse; padding-left: 30px;padding-top: 30px;padding-bottom: 30px;float:left;"
a href="http://#{App.url}"
= image_tag "images/logo-old.png", style: "display:inline-block", border: "0", alt: App.name, width: 139, height: 44, title: App.name
I tried the following already:
= image_tag "logo-old.png"
= image_tag "assets/images/logo-old.png"
= image_url "logo-old.png"
Nothing's working but good thing its now showing the Alt to know its still working by its path.
The current = image_tag evaluates to :
src="https://ci5.googleusercontent.com/proxy/83z0D3PVOqx-FwFFMwztfYBs5CSWHiURyxSUP6cZ3dq7Zo47k9mNPgotrijVmmWGxPHqblRTGCePqN4RrfEhHh_665
MdAQ=s0-d-e1-ft#http://localhost:3000/images/images/logo-old.png"
Any idea why the app can't find the normal pipeline path?
Instead of images/images/logo-old.png I need the assets/images/logo-old.png
Edit:
So for result comparison, if I do image_url "logo-old.png" the path results to /assets/logo-old.png
If I do image_url "images/logo-old.png" the path results to images/images/logo-old.png
My image is in app/assets/images/logo-old.png
1) is http://localhost:3000/images/images/logo-old.png the correct folder structure of your app/assets/images/images/logo-old.png ?
2) did you add the app/assets/images to the asset pipeline?
How to add folders to asset pipeline
Javascript does not work from a bootstrap template in rails
3) did you check with Rails.application.config.assets.paths the default search paths in your rails console, did you try to organize correctly the images to be stored inside app/assets/images folder and added that folder to the asset pipeline?
then you should be able to do image_url "logo-old.png"
Thanks!

How to load a marker png from within leaflet that is held in assets/images of a production site

My rails site is using the leaflet gem. I have no trouble loading the png in development, but when I switch to my production site the path now start under leaflet.
I want my path to by MY_WEB_SITE/assets/images/ but in production it becomes MY_WEB_SITE/assets/leaflet/dist/images/
I've attached .erb to my coffeescript. And the following code:
digested_icon = L.icon({
iconUrl: "<%= asset_path 'marker-icon.png' %>"
iconRetinaUrl: "<%= asset_path 'marker-icon-2x.png' %>"
shadowUrl: "<%= asset_path 'marker-shadow.png' %>"
})
sgMarker = L.marker(sgLatLng, {icon: digested_icon})
I've also tried asset_url, but it has the same effect.
Configure asset pipeline to compile .png files (particularly in vendor/ as it's not done by default):
# In config/initializers/assets.rb
Rails.application.config.assets.precompile += ["*.png"]
This will allow your .png images to be compiled to public/assets/leaflet/dist/images/ then they will show up in production.

Bootstrap-Sass: Glyphicons showing as rectangles on production

See the top right "admin" icon on http://base.sirius.uberspace.de/en.
When setting
config.assets.compile = true
they're showing correctly, but I guess that's not the perfect solution, is it?
Setting
config.assets.precompile += %w( '.woff', '.eot', '.svg', '.ttf' )
doesn't change anything; my mina deployment also does a precompile, as suggested:
invoke :'rails:assets_precompile'
So how could this be solved?
I'm using Rails 4.1.

In a Rails 4 app, how do I compile the jQuery Mobile button icons?

I am developing a rails 4 app with jQuery Mobile and using the jquery_mobile_rails gem which means I don't need to install any of the jQuery files. My problem is that there are no icons for the buttons. These are displayed in development but not in production. I assume that I just need to compile them but where are they and how can I do that?
Since I am not using jQuery Mobile files directly, I don't have the option to to store the icons below them. The gem works in development mode but not in production mode. Can I assume that the gems contain the button icons internally? If so, I am at a loss to understand why they don't work in production mode.
jquery-rails (2.3.0)
jquery_mobile_rails (1.3.0)
There is a known issue currently with Rails 4 when precompiling assets per environment.
Try setting:
config.assets.precompile=true
in config/application.rb.
If that still doesn't work, try adding the following to config/application.rb:
config.assets.precompile += %w(*.png *.jpg *.jpeg *.gif)
I can't replicate the strange error you get when concatenating these files to config.assets.precompile. Can you try the answer to this question instead (replace the line above):
config.assets.precompile << Proc.new { |path|
if path =~ /\.(css|js|png|jpg|jpeg|gif)\z/
full_path = Rails.application.assets.resolve(path).to_path
app_assets_path = Rails.root.join('app', 'assets').to_path
vendor_assets_path = Rails.root.join('vendor', 'assets').to_path
if ((full_path.starts_with? app_assets_path) || (full_path.starts_with? vendor_assets_path)) && (!path.starts_with? '_')
puts "\t" + full_path.slice(Rails.root.to_path.size..-1)
true
else
false
end
else
false
end
}

Conditional javascript require in the asset pipeline

I'm struggling with the asset pipeline. I'm loading dojo from Google CDN putting this in my template:
= javascript_include_tag 'http://ajax.googleapis.com/ajax/libs/dojo/1.6.1/dojo/dojo.xd.js', :'data-dojo-config' => %Q(dojoBlankHtmlUrl:'/blank.html', baseUrl: 'assets/', modulePaths: {custom: 'javascripts/modules'})
I just want a fallback to a local version if running locally or if the CDN is down. I thought of doing this:
script typeof(dojo) === "undefined" && document.write(unescape('%3Cscript src="js/libs/dojo-1.6.1.min.js"%3E%3C/script%3E'));
But I don't like it as it works out of the asset pipeline. I want to keep dojo in vendors/assets/javascripts/dojo. How can I get the fallback to be served by the asset pipeline.
Is there a way do declare conditional require in the asset pipeline. What I want is to run some javascript tests, and depending on the result serve a file.
Thanks
I suggest you use yepnope, a lightweight library for loading libraries like this in parallel (for speed) and it gives you the option to run some other code to test if the library is loaded. For example:
yepnope([{
load: 'http://ajax.googleapis.com/ajax/libs/dojo/1.6.1/dojo/dojo.xd.js',
complete: function () {
if (!window.jQuery) {
yepnope('asset_path('you_local_copy_of_dojo') ');
}
}
}])
(Note: You will need erb tags around the asset_path helper)
The local dojo file would be in the assets/javascript folder, but not included in the application manifest. You need to add the dojo file to the precompile array:
config.assets.precompile += 'your_local_file.js'
And this will make it available to the asset_path helper.
Thanks Richard!
I don't want to have yepnope to load one library. It would be overkill imo. Here is the solution I came up with, based on your help (written in slim):
1/ In vendors/assets/javascripts/, I have my dojo.js.
2/ In config/application.rb:
# Precompile these assets files
config.assets.precompile += ['dojo.js']
3/ In the template:
= javascript_include_tag "http://ajax.googleapis.com/ajax/libs/dojo/#{Settings.dojoVersion}/dojo/dojo.xd.js", :'data-dojo-config' => %Q(dojoBlankHtmlUrl:'/blank.html', baseUrl: 'assets/', modulePaths: {custom: 'javascripts/modules'})
script = "typeof(dojo) === \"undefined\" && document.write(unescape('%3Cscript src=\"#{asset_path('dojo')}\"%3E%3C/script%3E'));".html_safe
I also posted on the Rails Google Group to request the addition of two options to the javascript_include_tag, :test and :local that would take care of all the work. We'll see.

Resources