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

发布于 2024-10-19 19:23:49 字数 495 浏览 3 评论 0原文

我在这里做一个引擎,它独立工作正常。

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

这是 gemspec:

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

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

如果我手动插入这些 gem,在应用程序 Gemfile 中,一切正常......

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.

Here is the 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.

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

撩心不撩汉 2024-10-26 19:23:49

将它们包含在您的 gemfile 中并运行捆绑安装。然后在您的 lib//engine.rb 文件中需要它们。不要忘记需要 ruby​​gems

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

然后在你的主机应用程序(包含 gem 的应用程序)中运行bundle install/bundle update(bundle update 对我来说很有效),然后一切都应该完美运行。您还可以通过在主机应用程序中启动控制台并输入模块名称来测试这一点,例如

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

希望这有帮助

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

  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.

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

Hope this helps

微暖i 2024-10-26 19:23:49

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

  • yourengine.gemspec

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

    # 从 yourengine.gemspec 导入依赖项
    宝石规格
    
  • lib/yourengine.rb

    # 需要所有依赖项
    Gem.loaded_specs['yourengine'].dependency.each 做 |d|
     需要 d.name
    结尾
    
    需要“你的引擎/引擎”
    
    模块你的引擎
    结尾
    

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

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

  • yourengine.gemspec

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

    # Imports dependencies from yourengine.gemspec
    gemspec
    
  • 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)

自在安然 2024-10-26 19:23:49

来自 paperclip 的 README

对于非 Rails 使用:

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

我遇到了同样的问题,并为我解决了它。

from paperclip's README :

For Non-Rails usage:

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

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

猫腻 2024-10-26 19:23:49

您必须将 gem 文件添加到 .gemspec 文件和 engine.rb 文件中。
在 .gemspec 文件中,它会是这样的:
s.add_dependency "kaminari", "0.16.1"

在engine.rb文件顶部添加:
require "kaminari"

我认为您还需要将 gem 添加到 Rails 引擎 Gemfile 并捆绑安装,但我不确定您是否需要它。

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"

In the engine.rb file at the top add:
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.

总以为 2024-10-26 19:23:49

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

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

在 simple_view_helpers.gemspec 中声明 gem 的依赖项。
Bundler 将像基本依赖项一样对待运行时依赖项,并且
开发依赖项将默认添加到 :development 组中。

在此处声明仍在开发中的任何依赖项,而不是在
您的宝石规格。这些可能包括您路径中的边缘导轨或宝石,或者
吉特。请记住在发布之前将这些依赖项移至您的 gemspec
您的 gem 到 ruby​​gems.org。

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:

Quote from test/dummy/Gemfile (generated using 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.

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.

陈独秀 2024-10-26 19:23:49

您确实不应该在 Gemsec 上需要它们,并且应该加载它们。当您说“这是 gemspec”时,您是用 Gem::Specification.new do |s| 或类似的东西包围它,对吗?

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?

英雄似剑 2024-10-26 19:23:49

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

Bundler.require(*Rails.groups)

您可以将其添加到 config/initializer 中。

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

Bundler.require(*Rails.groups)

You could add this to an config/initializer.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文