Rails引擎 - Gems依赖项,如何将它们加载到应用程序中?

时间:2021-08-14 07:16:24

I'm doing an engine here, it works alright in stand alone.

我在这里做引擎,它可以独立工作。

When I transform it into a gem, and load it inside another application, I get a lot of undefined errors, coming from my engine gem's dependecies.

当我将它转换为gem并将其加载到另一个应用程序中时,我得到了很多未定义的错误,来自我的引擎gem的依赖。

Here is the gemspec:

这是gemspec:

s.add_dependency('paperclip')
s.add_dependency('jquery-rails')
s.add_dependency('rails3-jquery-autocomplete')
s.add_dependency('remotipart')
s.add_dependency('cancan')

In the application, when I do a bundle install, it lists all these dependencies, but as i run the application I receive a lot of undefined methods errors (has_attachment from paperclip for example). It seems that the application doesn't load the engines dependencies. Is this the default behavior? Can I change it? Same thing happened with a plugin inside the engine.

在应用程序中,当我进行bundle安装时,它列出了所有这些依赖项,但是当我运行应用程序时,我收到了很多未定义的方法错误(例如来自paperclip的has_attachment)。似乎应用程序不加载引擎依赖项。这是默认行为吗?我可以改变吗?引擎内部的插件也发生了同样的事情。

If I insert by hand those gems, in the application Gemfile, all works...

如果我手动插入那些宝石,在应用程序Gemfile中,所有工作......

7 个解决方案

#1


71  

Include them in your gemfile and run bundle install. Then require them in your lib/<your_engine>/engine.rb file. Don't forget to require rubygems

将它们包含在gemfile中并运行bundle install。然后在lib / /engine.rb文件中要求它们。别忘了要求rubygems

  require 'rubygems'
  require 'paperclip'
  require 'jquery-rails'
  require 'rails3-jquery-autocomplete'
  require 'remotipart'
  require 'cancan'

Then in your host app (The app where you included your gem) run bundle install/ bundle update (bundle update did the trick for me) and then everything should work perfectly. You can also test this by starting the console in your host app and just type the module name e.g.

然后在你的主机应用程序(你包含你的宝石的应用程序)运行bundle install / bundle update(bundle更新为我做了窍门)然后一切都应该完美。您还可以通过在主机应用中启动控制台来测试此功能,然后输入模块名称,例如

Loading development environment (Rails 3.0.3)
irb(main):001:0> Paperclip
=> Paperclip

Hope this helps

希望这可以帮助

#2


23  

You can require them manually like Daniel posted, and you can also require them automatically. You need to add dependencies in 3 files:

你可以像Daniel发布的那样手动要求它们,你也可以自动要求它们。您需要在3个文件中添加依赖项:

  • yourengine.gemspec

    yourengine.gemspec

    s.add_dependency "rails", '4.1.0'
    s.add_dependency "sqlite3"
    
  • Gemfile

    的Gemfile

    # Imports dependencies from yourengine.gemspec
    gemspec
    
  • lib/yourengine.rb

    LIB / yourengine.rb

    # requires all dependencies
    Gem.loaded_specs['yourengine'].dependencies.each do |d|
     require d.name
    end
    
    require 'yourengine/engine'
    
    module Yourengine
    end
    

Update: It's a simplistic demonstration of how to require the dependencies. You should test it and filter unwanted items, for example: require d.name unless d.type == :development (thx @imsinu9)

更新:这是如何要求依赖项的简单演示。您应该测试它并过滤不需要的项目,例如:require d.name除非d.type ==:development(thx @ imsinu9)

#3


1  

from paperclip's README :

来自paperclip的自述文件:

For Non-Rails usage:

对于非Rails用法:

class ModuleName < ActiveRecord::Base
  include Paperclip::Glue
  ...
end

I had the same issue and that fixed it for me.

我有同样的问题,并为我修复它。

#4


1  

You must add the gem file to both the .gemspec file, and your engine.rb file. In the .gemspec file it would be like: s.add_dependency "kaminari", "0.16.1"

您必须将gem文件添加到.gemspec文件和engine.rb文件中。在.gemspec文件中它将是:s.add_dependency“kaminari”,“0.16.1”

In the engine.rb file at the top add: require "kaminari"

在顶部的engine.rb文件中添加:require“kaminari”

I think you also need to add the gem to the rails engine Gemfile and bundle install, but I'm not certain if you need it there.

我想你还需要将gem添加到rails引擎Gemfile和bundle install中,但我不确定你是否需要它。

#5


0  

At the time being (Rails 3.1 and above I think), you shouldn't have do declare any gems in the test/dummy/Gemfile anymore:

目前(我认为是Rails 3.1及以上版本),您不应再在test / dummy / Gemfile中声明任何gem:

Quote from test/dummy/Gemfile (generated using rails plugin new my_engine --full):

从test / dummy / Gemfile引用(使用rails plugin new my_engine --full生成):

Declare your gem's dependencies in simple_view_helpers.gemspec. Bundler will treat runtime dependencies like base dependencies, and development dependencies will be added by default to the :development group.

在simple_view_helpers.gemspec中声明gem的依赖项。 Bundler将处理运行时依赖性,如基本依赖性,默认情况下,开发依赖项将添加到:development组。

Declare any dependencies that are still in development here instead of in your gemspec. These might include edge Rails or gems from your path or Git. Remember to move these dependencies to your gemspec before releasing your gem to rubygems.org.

声明仍在开发中的任何依赖项,而不是在gemspec中。这些可能包括路径或Git中的边缘Rails或宝石。在将gem发布到rubygems.org之前,请记住将这些依赖项移到gemspec中。

#6


0  

You really shouldn't need them on the Gemsec, and they should be loaded. When you say "here is the gemspec", you are surrounding it with Gem::Specification.new do |s| or something to that effect, right?

你真的不应该在Gemsec上需要它们,它们应该被加载。当你说“这里是gemspec”时,你用Gem :: Specification.new do | s |包围它或者那种效果,对吧?

#7


0  

You can include all gems for the environment with a simple bundler command:

您可以使用简单的bundler命令包含环境的所有gem:

Bundler.require(*Rails.groups)

You could add this to an config/initializer.

您可以将其添加到配置/初始化程序。

#1


71  

Include them in your gemfile and run bundle install. Then require them in your lib/<your_engine>/engine.rb file. Don't forget to require rubygems

将它们包含在gemfile中并运行bundle install。然后在lib / /engine.rb文件中要求它们。别忘了要求rubygems

  require 'rubygems'
  require 'paperclip'
  require 'jquery-rails'
  require 'rails3-jquery-autocomplete'
  require 'remotipart'
  require 'cancan'

Then in your host app (The app where you included your gem) run bundle install/ bundle update (bundle update did the trick for me) and then everything should work perfectly. You can also test this by starting the console in your host app and just type the module name e.g.

然后在你的主机应用程序(你包含你的宝石的应用程序)运行bundle install / bundle update(bundle更新为我做了窍门)然后一切都应该完美。您还可以通过在主机应用中启动控制台来测试此功能,然后输入模块名称,例如

Loading development environment (Rails 3.0.3)
irb(main):001:0> Paperclip
=> Paperclip

Hope this helps

希望这可以帮助

#2


23  

You can require them manually like Daniel posted, and you can also require them automatically. You need to add dependencies in 3 files:

你可以像Daniel发布的那样手动要求它们,你也可以自动要求它们。您需要在3个文件中添加依赖项:

  • yourengine.gemspec

    yourengine.gemspec

    s.add_dependency "rails", '4.1.0'
    s.add_dependency "sqlite3"
    
  • Gemfile

    的Gemfile

    # Imports dependencies from yourengine.gemspec
    gemspec
    
  • lib/yourengine.rb

    LIB / yourengine.rb

    # requires all dependencies
    Gem.loaded_specs['yourengine'].dependencies.each do |d|
     require d.name
    end
    
    require 'yourengine/engine'
    
    module Yourengine
    end
    

Update: It's a simplistic demonstration of how to require the dependencies. You should test it and filter unwanted items, for example: require d.name unless d.type == :development (thx @imsinu9)

更新:这是如何要求依赖项的简单演示。您应该测试它并过滤不需要的项目,例如:require d.name除非d.type ==:development(thx @ imsinu9)

#3


1  

from paperclip's README :

来自paperclip的自述文件:

For Non-Rails usage:

对于非Rails用法:

class ModuleName < ActiveRecord::Base
  include Paperclip::Glue
  ...
end

I had the same issue and that fixed it for me.

我有同样的问题,并为我修复它。

#4


1  

You must add the gem file to both the .gemspec file, and your engine.rb file. In the .gemspec file it would be like: s.add_dependency "kaminari", "0.16.1"

您必须将gem文件添加到.gemspec文件和engine.rb文件中。在.gemspec文件中它将是:s.add_dependency“kaminari”,“0.16.1”

In the engine.rb file at the top add: require "kaminari"

在顶部的engine.rb文件中添加:require“kaminari”

I think you also need to add the gem to the rails engine Gemfile and bundle install, but I'm not certain if you need it there.

我想你还需要将gem添加到rails引擎Gemfile和bundle install中,但我不确定你是否需要它。

#5


0  

At the time being (Rails 3.1 and above I think), you shouldn't have do declare any gems in the test/dummy/Gemfile anymore:

目前(我认为是Rails 3.1及以上版本),您不应再在test / dummy / Gemfile中声明任何gem:

Quote from test/dummy/Gemfile (generated using rails plugin new my_engine --full):

从test / dummy / Gemfile引用(使用rails plugin new my_engine --full生成):

Declare your gem's dependencies in simple_view_helpers.gemspec. Bundler will treat runtime dependencies like base dependencies, and development dependencies will be added by default to the :development group.

在simple_view_helpers.gemspec中声明gem的依赖项。 Bundler将处理运行时依赖性,如基本依赖性,默认情况下,开发依赖项将添加到:development组。

Declare any dependencies that are still in development here instead of in your gemspec. These might include edge Rails or gems from your path or Git. Remember to move these dependencies to your gemspec before releasing your gem to rubygems.org.

声明仍在开发中的任何依赖项,而不是在gemspec中。这些可能包括路径或Git中的边缘Rails或宝石。在将gem发布到rubygems.org之前,请记住将这些依赖项移到gemspec中。

#6


0  

You really shouldn't need them on the Gemsec, and they should be loaded. When you say "here is the gemspec", you are surrounding it with Gem::Specification.new do |s| or something to that effect, right?

你真的不应该在Gemsec上需要它们,它们应该被加载。当你说“这里是gemspec”时,你用Gem :: Specification.new do | s |包围它或者那种效果,对吧?

#7


0  

You can include all gems for the environment with a simple bundler command:

您可以使用简单的bundler命令包含环境的所有gem:

Bundler.require(*Rails.groups)

You could add this to an config/initializer.

您可以将其添加到配置/初始化程序。