Unable to require a js file from a gem in asset pipeline - ruby-on-rails

I have a local gem. And there is a js file:
app/assets/javascripts/autocomplete_location/location_autocomplete.js
Now I am trying to use this gem in my rails project. I am able to use it's other ruby files, but can't include the above js file. I have tried putting engine.rb file in my gem. Still no luck.
I tried
//= require autocomplete_locations/location_autocomplete
in application.js file of my project. Still, when I start server and open any page, it throws this error:
Sprockets::FileNotFound > couldn't find file 'autocomplete_locations/location_autocomplete' with type 'application/javascript'

//= require autocomplete_locations/location_autocomplete is still not the same as 'autocomplete_locations/location_autocomplete. The folder being required is autocomplete_locations and the name of the folder containing the file is autocomplete_location. I didn't catch that neither on my previous comment. If that does not solve the issue. Try requiring and testing a different javascript file just to make sure it is not a problem with the asset pipeline.

Thanks for your suggestions, after struggling through several hit and trial, I solved this by adding below code in lib/autocomplete_locations.rb:
class Engine < ::Rails::Engine
environment = Sprockets::Environment.new
environment.append_path '../vendor/assets/javascripts'
end
Now I am able to require my assets in rails project.

Related

Sprockets::DoubleLinkError: Multiple files with the same output path cannot be linked ("application.js") in Rails 7

Sprockets::DoubleLinkError: Multiple files with the same output path cannot be linked ("application.js") in Rails 7.
After compiling assets:
Here is my manifest.json:
Directory structure:
I had this error message before and this comment from DHH helped me resolve it.
You have to rename one of the two files. They're both being generated at the same path which doesn't work. Normally if you use builds/application.js you don't also need app/assets/javascripts/application.js.
I renamed app/assets/javascripts/application.js to app/assets/javascripts/application.assets.js and the error resolves until the server restarts and the build asset is generated again.
Removing:
//= link_tree ../../javascript .js
from manifest.js resolved appears to actually resolve the issue. Since you don't need to load application.js if you are loading the builds folder.

How can I include javascript file in the rails assets pipeline?

I have created a new ruby on rails project using the command : rails new [project_name].
Further, I want to create javascript files, so I can control my view elements.
For this, I've created a new js file and placed it in the app->assets->javascripts->apple_pay.js
Currently the appe_pay.js implementation is just for testing :
console.log("test")
Also, my application.js, used like a manifest file, looks like this :
//= require_tree .
When I run the project using rails server command, I'm expecting to see the message displayed on the console, but it's not, any ideea ?
find: config/initializers/assets.rb
Then: Rails.application.config.assets.precompile += %w[your_file.js]
noticing that as it's out of application.js you need to include it inside your views using:
javascript_include_tag :your_file
Save & Restart your server :)
If you are not install "jquery-rails" then first install the gem and require file also add in application.js
//= require file_name

Rails 5 can't find JS file

I installed a plugin (http://antenna.io/demo/jquery-bar-rating/examples/) using Bower.
Now I have a file:
vendor/assets/bower_components/jquery-bar-rating/jquery.barrating.js
Also in my application.rb I have:
config.assets.paths << Rails.root.join('vendor', 'assets', 'bower_components')
I restarted my Rails server but when I access the app it says:
Sprockets::FileNotFound: couldn't find file 'jquery.barrating' with type 'application/javascript'
Checked in these paths:
/Users/xxx/Documents/Dev/xxx/Backend/app/assets/documentation
/Users/xxx/Documents/Dev/xxx/Backend/app/assets/images
/Users/xxx/Documents/Dev/xxx/Backend/app/assets/javascripts
/Users/xxx/Documents/Dev/xxx/Backend/app/assets/stylesheets
/Users/xxx/Documents/Dev/xxx/Backend/vendor/assets/bower_components
So it looks in the right folder but still can't find the file.
Any idea what is happening?
I can't entirely remember how I did it a while ago, but I think basically if you need the file in the plugin within your js, you could do:
//= require jquery-bar-rating
//OR
//= require jquery-bar-rating/jquery.barrating

Rails 3.1 engine not loading vendor assets

I have a super simple engine for loading some vendored assets: https://github.com/febuiles/strap_on
I include it in my application's Gemfile like this: gem "strap_on".
In my application.css file I have: *= require twitter_bootstrap.
I start the application and I get couldn't find file 'twitter_bootstrap'.
I've notice that the path for the engine is not in Rails.application.config.assets.paths. Any idea of what I might be doing wrong here?
In your gemspec file - it doesnt look like you include files from the vendor directory, guessing that is your main issue, check asset path after adding and reinstalling - if you look in your gem repository now, i am guessing the files in vendor are not included
Also i would move twitter_bootstrap.js into the twiiter_bootstrap directory and rename it index.js, not sure if this is required or not (you will need to update paths in that file also)
You might also be interested in this post? http://house9.blogspot.com/2011/06/rails-31-asset-gem.html

Include css and js from an engine to a rails app created with rails plugin new

I just created a rails plugin with "rails plugin new pluginname --mountable".
Now I would like my app in test/dummy to access the js and css in my app/assets/javascripts and app/assets/stylesheets directories... I would like this process to be "automatic"(so no copy/paste of files from these directories to my test/dummy...) because I would like to release this plugin for people to use it and it should be as much easy to use as possible.
Thank you in advance for your help
Gnagno
The asset pipeline will find any asset that is inside of an engine. If you have a file app/assets/javascripts/foo.js and in your host application's application.js has:
//= require foo
It will include your asset in the combined application.js.
If you want to automatically include your file you will have to add to config.action_view.javascript_expansions[:defaults] but this was replaced with using comment requires in the asset pipeline.

Resources