izaurio / js_assets Goto Github PK
View Code? Open in Web Editor NEWJavascript helper in rails projects
License: MIT License
Javascript helper in rails projects
License: MIT License
After Rails upgrade, I notice the assets always have digests, even if config.assets.digest = false
is set in development.rb.
if env.file_digest(filename)
I'm not sure why, but I think that's true even if digest is off. So I guess the condition could be changed to check the config directly.
I have a staging environments. But in code env-s only production and development. In my environment i dont have any paths
We are using this gem inside one of our rails apps and I just updated to the latest release of the sass-rails
gem, which in turn bumped the sprockets version in our Gemfile.lock to 3.0.3
, and then when I went to reboot the server I got this message:
/usr/local/lib/ruby/gems/2.1.0/gems/js_assets-0.0.9/lib/js_assets/list.rb:12:in `block in fetch': undefined method `logical_path_for_filename' for #<Sprockets::Environment:0x007f84100e06f0> (NoMethodError)
from /usr/local/lib/ruby/gems/2.1.0/gems/sprockets-3.0.3/lib/sprockets/paths.rb:73:in `block (2 levels) in each_file'
from /usr/local/lib/ruby/gems/2.1.0/gems/sprockets-3.0.3/lib/sprockets/path_utils.rb:223:in `block in stat_tree'
from /usr/local/lib/ruby/gems/2.1.0/gems/sprockets-3.0.3/lib/sprockets/path_utils.rb:207:in `block in stat_directory'
from /usr/local/lib/ruby/gems/2.1.0/gems/sprockets-3.0.3/lib/sprockets/path_utils.rb:204:in `each'
from /usr/local/lib/ruby/gems/2.1.0/gems/sprockets-3.0.3/lib/sprockets/path_utils.rb:204:in `stat_directory'
from /usr/local/lib/ruby/gems/2.1.0/gems/sprockets-3.0.3/lib/sprockets/path_utils.rb:222:in `stat_tree'
from /usr/local/lib/ruby/gems/2.1.0/gems/sprockets-3.0.3/lib/sprockets/paths.rb:71:in `each'
from /usr/local/lib/ruby/gems/2.1.0/gems/sprockets-3.0.3/lib/sprockets/paths.rb:71:in `block in each_file'
from /usr/local/lib/ruby/gems/2.1.0/gems/sprockets-3.0.3/lib/sprockets/paths.rb:70:in `each'
from /usr/local/lib/ruby/gems/2.1.0/gems/sprockets-3.0.3/lib/sprockets/paths.rb:70:in `each_file'
from /usr/local/lib/ruby/gems/2.1.0/gems/js_assets-0.0.9/lib/js_assets/list.rb:11:in `fetch'
from /usr/local/lib/ruby/gems/2.1.0/gems/js_assets-0.0.9/app/assets/javascripts/app_assets.js.erb:1:in `_evaluate_template'
from /usr/local/lib/ruby/gems/2.1.0/gems/sprockets-3.0.3/lib/sprockets/erb_processor.rb:26:in `call'
from /usr/local/lib/ruby/gems/2.1.0/gems/sprockets-3.0.3/lib/sprockets/erb_processor.rb:13:in `call'
from /usr/local/lib/ruby/gems/2.1.0/gems/sprockets-3.0.3/lib/sprockets/processor_utils.rb:75:in `call_processor'
from /usr/local/lib/ruby/gems/2.1.0/gems/sprockets-3.0.3/lib/sprockets/processor_utils.rb:57:in `block in call_processors'
from /usr/local/lib/ruby/gems/2.1.0/gems/sprockets-3.0.3/lib/sprockets/processor_utils.rb:56:in `reverse_each'
from /usr/local/lib/ruby/gems/2.1.0/gems/sprockets-3.0.3/lib/sprockets/processor_utils.rb:56:in `call_processors'
from /usr/local/lib/ruby/gems/2.1.0/gems/sprockets-3.0.3/lib/sprockets/loader.rb:87:in `load_asset_by_uri'
from /usr/local/lib/ruby/gems/2.1.0/gems/sprockets-3.0.3/lib/sprockets/loader.rb:45:in `block in load'
from /usr/local/lib/ruby/gems/2.1.0/gems/sprockets-3.0.3/lib/sprockets/loader.rb:157:in `fetch_asset_from_dependency_cache'
from /usr/local/lib/ruby/gems/2.1.0/gems/sprockets-3.0.3/lib/sprockets/loader.rb:38:in `load'
from /usr/local/lib/ruby/gems/2.1.0/gems/sprockets-3.0.3/lib/sprockets/cached_environment.rb:20:in `block in initialize'
from /usr/local/lib/ruby/gems/2.1.0/gems/sprockets-3.0.3/lib/sprockets/cached_environment.rb:47:in `yield'
from /usr/local/lib/ruby/gems/2.1.0/gems/sprockets-3.0.3/lib/sprockets/cached_environment.rb:47:in `load'
from /usr/local/lib/ruby/gems/2.1.0/gems/sprockets-3.0.3/lib/sprockets/bundle.rb:23:in `block in call'
from /usr/local/lib/ruby/gems/2.1.0/gems/sprockets-3.0.3/lib/sprockets/utils.rb:183:in `dfs'
from /usr/local/lib/ruby/gems/2.1.0/gems/sprockets-3.0.3/lib/sprockets/bundle.rb:24:in `call'
from /usr/local/lib/ruby/gems/2.1.0/gems/sprockets-3.0.3/lib/sprockets/processor_utils.rb:75:in `call_processor'
from /usr/local/lib/ruby/gems/2.1.0/gems/sprockets-3.0.3/lib/sprockets/processor_utils.rb:57:in `block in call_processors'
from /usr/local/lib/ruby/gems/2.1.0/gems/sprockets-3.0.3/lib/sprockets/processor_utils.rb:56:in `reverse_each'
from /usr/local/lib/ruby/gems/2.1.0/gems/sprockets-3.0.3/lib/sprockets/processor_utils.rb:56:in `call_processors'
from /usr/local/lib/ruby/gems/2.1.0/gems/sprockets-3.0.3/lib/sprockets/loader.rb:87:in `load_asset_by_uri'
from /usr/local/lib/ruby/gems/2.1.0/gems/sprockets-3.0.3/lib/sprockets/loader.rb:45:in `block in load'
from /usr/local/lib/ruby/gems/2.1.0/gems/sprockets-3.0.3/lib/sprockets/loader.rb:157:in `fetch_asset_from_dependency_cache'
from /usr/local/lib/ruby/gems/2.1.0/gems/sprockets-3.0.3/lib/sprockets/loader.rb:38:in `load'
from /usr/local/lib/ruby/gems/2.1.0/gems/sprockets-3.0.3/lib/sprockets/cached_environment.rb:20:in `block in initialize'
from /usr/local/lib/ruby/gems/2.1.0/gems/sprockets-3.0.3/lib/sprockets/cached_environment.rb:47:in `yield'
from /usr/local/lib/ruby/gems/2.1.0/gems/sprockets-3.0.3/lib/sprockets/cached_environment.rb:47:in `load'
from /usr/local/lib/ruby/gems/2.1.0/gems/sprockets-3.0.3/lib/sprockets/base.rb:63:in `find_asset'
from /usr/local/lib/ruby/gems/2.1.0/gems/sprockets-3.0.3/lib/sprockets/environment.rb:30:in `find_asset'
from /usr/local/lib/ruby/gems/2.1.0/gems/sprockets-3.0.3/lib/sprockets/base.rb:89:in `[]'
from /home/vagrant/.bundler/ruby/2.1.0/react-rails-99a1b38b28db/lib/react/rails/railtie.rb:68:in `block (3 levels) in <class:Railtie>'
from /home/vagrant/.bundler/ruby/2.1.0/react-rails-99a1b38b28db/lib/react/rails/railtie.rb:67:in `map'
from /home/vagrant/.bundler/ruby/2.1.0/react-rails-99a1b38b28db/lib/react/rails/railtie.rb:67:in `block (2 levels) in <class:Railtie>'
from /home/vagrant/.bundler/ruby/2.1.0/react-rails-99a1b38b28db/lib/react/renderer.rb:73:in `call'
from /home/vagrant/.bundler/ruby/2.1.0/react-rails-99a1b38b28db/lib/react/renderer.rb:73:in `setup_combined_js'
from /home/vagrant/.bundler/ruby/2.1.0/react-rails-99a1b38b28db/lib/react/renderer.rb:78:in `reset_combined_js!'
from /home/vagrant/.bundler/ruby/2.1.0/react-rails-99a1b38b28db/lib/react/renderer.rb:20:in `setup!'
from /home/vagrant/.bundler/ruby/2.1.0/react-rails-99a1b38b28db/lib/react/rails/railtie.rb:74:in `block (2 levels) in <class:Railtie>'
from /home/vagrant/.bundler/ruby/2.1.0/react-rails-99a1b38b28db/lib/react/rails/railtie.rb:78:in `call'
from /home/vagrant/.bundler/ruby/2.1.0/react-rails-99a1b38b28db/lib/react/rails/railtie.rb:78:in `block in <class:Railtie>'
from /usr/local/lib/ruby/gems/2.1.0/gems/activesupport-4.1.9/lib/active_support/lazy_load_hooks.rb:36:in `call'
from /usr/local/lib/ruby/gems/2.1.0/gems/activesupport-4.1.9/lib/active_support/lazy_load_hooks.rb:36:in `execute_hook'
from /usr/local/lib/ruby/gems/2.1.0/gems/activesupport-4.1.9/lib/active_support/lazy_load_hooks.rb:45:in `block in run_load_hooks'
from /usr/local/lib/ruby/gems/2.1.0/gems/activesupport-4.1.9/lib/active_support/lazy_load_hooks.rb:44:in `each'
from /usr/local/lib/ruby/gems/2.1.0/gems/activesupport-4.1.9/lib/active_support/lazy_load_hooks.rb:44:in `run_load_hooks'
from /usr/local/lib/ruby/gems/2.1.0/gems/railties-4.1.9/lib/rails/application/finisher.rb:64:in `block in <module:Finisher>'
from /usr/local/lib/ruby/gems/2.1.0/gems/railties-4.1.9/lib/rails/initializable.rb:30:in `instance_exec'
from /usr/local/lib/ruby/gems/2.1.0/gems/railties-4.1.9/lib/rails/initializable.rb:30:in `run'
from /usr/local/lib/ruby/gems/2.1.0/gems/railties-4.1.9/lib/rails/initializable.rb:55:in `block in run_initializers'
from /usr/local/lib/ruby/2.1.0/tsort.rb:226:in `block in tsort_each'
from /usr/local/lib/ruby/2.1.0/tsort.rb:348:in `block (2 levels) in each_strongly_connected_component'
from /usr/local/lib/ruby/2.1.0/tsort.rb:427:in `each_strongly_connected_component_from'
from /usr/local/lib/ruby/2.1.0/tsort.rb:347:in `block in each_strongly_connected_component'
from /usr/local/lib/ruby/2.1.0/tsort.rb:345:in `each'
from /usr/local/lib/ruby/2.1.0/tsort.rb:345:in `call'
from /usr/local/lib/ruby/2.1.0/tsort.rb:345:in `each_strongly_connected_component'
from /usr/local/lib/ruby/2.1.0/tsort.rb:224:in `tsort_each'
from /usr/local/lib/ruby/2.1.0/tsort.rb:205:in `tsort_each'
from /usr/local/lib/ruby/gems/2.1.0/gems/railties-4.1.9/lib/rails/initializable.rb:54:in `run_initializers'
from /usr/local/lib/ruby/gems/2.1.0/gems/railties-4.1.9/lib/rails/application.rb:300:in `initialize!'
from /vagrant/config/environment.rb:5:in `<top (required)>'
from /usr/local/lib/ruby/gems/2.1.0/gems/railties-4.1.9/lib/rails/application.rb:276:in `require'
from /usr/local/lib/ruby/gems/2.1.0/gems/railties-4.1.9/lib/rails/application.rb:276:in `require_environment!'
from /usr/local/lib/ruby/gems/2.1.0/gems/railties-4.1.9/lib/rails/commands/commands_tasks.rb:147:in `require_application_and_environment!'
from /usr/local/lib/ruby/gems/2.1.0/gems/railties-4.1.9/lib/rails/commands/commands_tasks.rb:68:in `console'
from /usr/local/lib/ruby/gems/2.1.0/gems/railties-4.1.9/lib/rails/commands/commands_tasks.rb:40:in `run_command!'
from /usr/local/lib/ruby/gems/2.1.0/gems/railties-4.1.9/lib/rails/commands.rb:17:in `<top (required)>'
from bin/rails:8:in `require'
from bin/rails:8:in `<main>'
Correct me if I'm wrong, but I believe this gem is the one invoking sprockets. At any rate, limiting the max sprockets version to 2.x
on our Gemfile
stopped these errors from being raised.
The issue appears to be that the method in question, logical_path_for_filename
, existed on Sprockets 2.x but was removed at some point prior to the release of Sprockets 3.
I just did some digging... here is the commit moving it of the Sprockets::Environment
class.
Here's a commit making it private..
Here's a commit where the method is renamed to normalize_logical_path
At any rate I can't keep tracking it but it looks like for Sprockets 3 the appropriate method to call to find the path for something is Sprockets::Environment#resolve
from the Sprockets::Resolve
module.
For now I would release a v0.10 of this gem with a gem dependency limiter for sprockets 2.x and then look into making a new release to fix the feature. Sorry I can't be of more help but I am not very familiar with the Sprockets gem / the internals of the asset pipeline.
I am using a custom RAILS_ENV=staging for my QA environment, but I think this issue still exists if I set RAILS_ENV=production. I looked through your code for specific references to environment names and found that they had been removed, so I'm guessing this should work. (Maybe it's something addressed by the open pull request. Not sure.)
I have a little red "X" png that is part of my image assets, and it needs to be included in some javascript code that adds nested attributes as part of a workflow for file attachments. The file x_16x16.png
is in the asset pipeline.
$ RAILS_ENV=staging rails c
Loading staging environment (Rails 4.2.7.1)
2.3.0 :001 > ActionController::Base.helpers.asset_path('x_16x16.png')
=> "/assets/x_16x16-7d1b5578d7288518ff6184d04063cf116faf9ab5a52d7f19898af57280de5303.png"
However, I include this snippet:
content += "<td><a href='javascript: void(0);' onClick='remove_attachment(this," + i + ");'><img src='" + asset_path('x_16x16.png') + "'></a></td></tr>";
and it comes out like:
<a href="javascript: void(0);" onclick="remove_attachment(this,0);"><img src="/assets/x_16x16.png"></a>
Can you help? I have found no other way to include asset paths in javascript cleanly.
I get this error during precompilation:
NoMethodError: undefined method `logical_paths' for nil:NilClass
/usr/local/rvm/gems/ruby-2.2.2/gems/js_assets-0.1.0/lib/js_assets/list.rb:10:in `fetch'
/usr/local/rvm/gems/ruby-2.2.2/gems/js_assets-0.1.0/app/assets/javascripts/app_assets.js.erb:1:in `_evaluate_template'
This is the relevant line:
::Rails.application.assets.logical_paths do |logical_path, filename|
It's legal for Rails.application.assets
to be nil
in production:
https://github.com/rails/sprockets-rails
config.assets.compile
Enables Sprockets compile environment. If disabled, Rails.application.assets will be
nil
to prevent inadvertent compilation calls. View helpers will depend on assets being precompiled to public/assets in order to link to them. Initializers expecting Rails.application.assets during boot should be accessing the environment in a config.assets.configure block. See below.
A restart of the server is needed to get the new file detected. I believe this is due to the erb file being cached.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.