I'm creating a rails mountable engine plugin which uses the gem "jquery-rails". I added this code in .gemspec file
s.add_dependency "jquery-rails", "~> 3.0.1"
and run bundle install
, bundle update
. (BTW is this adding necessary? Since rails mountable engine already added "rails 4.0.1" which in turn required "jquery-rails 3.0.4" as its dependency from the start?).
In app/assets/javascript/mountable_engine_name/application.js
//= require jquery
//= require jquery-ujs
//= require_tree .
But when I run the server on test/dummy/ and access any template which uses the tag <%= javascript_include_tag "mountable_engine_name/application" %>
it's showing the error "couldn't find file 'jquery'".
I tried creating a brand new mountable engine plugin but it happens the same.
Did I do something wrong?
PS. Sorry for my English.
UPDATE
Actually it happens on all every manifest file which require jquery and jquery-ujs both in app/assets/javascript/ and test/dummy/app/assets/javascript/ .
This is about a problem every newbie starting off with Rails Engine face. I know its a question as dated back as 4 years as of today and its has an answer marked correct already. I just faced the same problem in Rails 5 when writing TestCases for my engine app, and I noticed I have to add require jquery
to all my engine's application.js
file, which violates rails DRY paradigm.
I feel the marked answer a little bit violates Rails DRY (Don't Repeat Yourself) paradigm.
In a Rails Engine based solution, application.js
is present in:
Solution:
look for your
.gemspec
file from your directory and add a dependency e.g.
s.add_dependency "jquery-rails", "~> 3.1.1"
cd
into the the root of your engine app. e.g.
cd engines/csv_importer/
Note: csv_importer
is the name of the app inside my engine.
install the added dependency by running:
bundle install
now the next thing is to
require query-rails
inside yourlib/engine.rb
.
require 'jquery-rails'
..in my own case, I added a simple initializer to engine.rb and require 'jquery-rails'
in there too. This makes me able to run other tasks from the domain/parent app such as bundling the engine app itself as a gem into the parent app. Requiring jquery-rails
there too makes 'jquery-rails' to be globally available inside my specific engine app. e.g. so I have:
require 'jquery-rails'
module CsvImporter #Note CsvImporter is the engine name/app in my own case
class Engine < ::Rails::Engine
isolate_namespace CsvImporter #Note CsvImporter is the engine name/app in my own case
# This enables me to be able to correctly migrate the database from the parent application.
initializer :append_migrations do |app|
unless app.root.to_s.match(root.to_s)
config.paths['db/migrate'].expanded.each do |p|
app.config.paths['db/migrate'] << p
end
end
end
end
end
start your rails app by running:
rails s
That is what I have done to solve the problem in my rails mountable engine plugin. I hope this helps a newbie coming in May 2018.
Thanks
I know the original question was asked a few months ago, but I ran into the same problem while working through the Rails engine tutorial, so I'm posting in case this helps others...
I found that I needed to update four files:
1.) add a dependency to your .gemspec file
#file: <your engine name>.gemspec
s.add_dependency "jquery-rails", "~> 3.1.1"
2.) update your engine application.js file:
#file: <your engine name>/app/assets/javascripts/blorgh/application.js
...
//= require jquery
//= require jquery_ujs
//= require_tree .
3.) update your parent app application.js file:
#file: <your engine name>/test/dummy/app/assets/javascripts/application.js
...
//= require jquery
//= require jquery_ujs
//= require_tree .
4.) per this SO thread, you need to add a require to your engine.rb file:
#file: lib/<your engine name>/engine.rb
module <your engine name>
class Engine < ::Rails::Engine
require 'jquery-rails'
isolate_namespace Blorgh
end
end
peace.
You should require jquery-rails in your gem's main file.
If you have a gem called "foo" do so in it's main file:
# lib/foo.rb
require "jquery-rails"
# ...
I have found necessary to include a reference in the Gemfile of the engine:
#Gemfile
gem "jquery-rails"
Also, the requirement of jquery ujs is done with an underscore:
//=require jquery_ujs
Best of luck!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With