可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
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/ .
回答1:
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.
回答2:
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"
# ...
回答3:
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!
回答4:
Add both runtime and development dependency:
s.add_runtime_dependency "jquery-rails", "~> 3.0.1"
s.add_development_dependency "jquery-rails", "~> 3.0.1"
This worked for me.
回答5:
I had the same problem.
When using engines you have two application.js
files. The one in the app and one in the engine.
It looks like you were trying to include jquery in a name-spaced directory, in parent app, which is not actually the engine.
Brief Context
Engines can be used in different ways, so my approach is nesting engines in the parent app (which I called apps
) like so
PARENT_APP_NAME/apps/ENGINE_NAME/app/assets/javascripts/ENGINE_NAME/application.js
That is where you need to include the
//= require jquery
回答6:
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:
- the Domain Asset Directory;
- all your Engine Asset Directories (i.e. in any case you have more than one app in that engine)
- all your Engine Test Dummy Directories (i.e. in any case you have more than one app in that engine)
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 your lib/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