【问题标题】:Rails3 Engine: Gems (paperclip and inherited_resources) not loadedRails3 引擎:未加载宝石(回形针和继承的资源)
【发布时间】:2011-06-28 19:11:35
【问题描述】:

在我的 rails3 引擎 gemfile 中,我添加了:

gem 'paperclip'
gem 'inherited_resources'

我也将此添加到我的 gemspec 中:

s.add_dependency "paperclip"
s.add_dependency "inherited_resources"

然后我为我的引擎和客户端应用程序运行 bundle install 并启动客户端应用程序的控制台以首先测试我得到的回形针:

Using /Users/ynkr/.rvm/gems/ruby-1.9.2-p180 with gemset rails31beta
ynkr % rails c
/Users/ynkr/.rvm/gems/ruby-1.9.2-p180@rails31beta/gems/actionpack-3.1.0.rc4/lib/action_dispatch/http/mime_type.rb:101: warning: already initialized constant YML
Loading development environment (Rails 3.1.0.rc4)
ruby-1.9.2-p180 :001 > b=Blog::Blog.first 
  Blog::Blog Load (0.2ms)  SELECT `blogs`.* FROM `blogs` LIMIT 1
 => #<Blog::Blog id: 1, user_id: 1, context_id: 2, title: "Cmd Line Blog Title", title_for_url: "cmd-line-blog-title", teaser: "This is the teaser for the command line blog", content: "Some content for the cmd line blog", created_at: "2011-06-28 06:06:55", updated_at: "2011-06-28 06:06:55"> 
ruby-1.9.2-p180 :002 > b.photos
NoMethodError: undefined method `has_attached_file' for #<Class:0x00000102a57fc8>
    from /Users/ynkr/.rvm/gems/ruby-1.9.2-p180@rails31beta/gems/activerecord-3.1.0.rc4/lib/active_record/base.rb:1078:in `method_missing'
    from /websites/gems/blog/app/models/blog/photo.rb:6:in `<class:Photo>'
    from /websites/gems/blog/app/models/blog/photo.rb:1:in `<top (required)>'
    from /Users/ynkr/.rvm/gems/ruby-1.9.2-p180@rails31beta/gems/activesupport-3.1.0.rc4/lib/active_support/dependencies.rb:452:in `load'

好的,所以似乎找不到回形针 gem 中的 has_attached_file 方法。为什么?我不确定。

继续讨论inherited_resources,我将脚手架生成的控制器更改为如下所示:

class Blog::ContextsController < InheritedResources::Base
  before_filter :redirect_unless_admin
end

并加载我(在日志中)受到欢迎的索引页面:

Started GET "/blog/contexts" for 127.0.0.1 at 2011-06-28 12:06:25 -0700

ActionController::RoutingError (uninitialized constant InheritedResources):

Rendered /Users/ynkr/.rvm/gems/ruby-1.9.2-p180@rails31beta/gems/actionpack-3.1.0.rc4/lib/action_dispatch/middleware/templates/rescues/routing_error.erb within rescues/layout (0.5ms)

我的问题是为什么这些不起作用?我的直觉是,我完全不知道关于引擎、命名空间和加载 gems 的一些事情。

【问题讨论】:

    标签: ruby-on-rails ruby-on-rails-3 gem rails-engines


    【解决方案1】:

    您需要在初始化文件中要求引擎中的每个 gem,例如。 lib/&lt;your_engine_name&gt;/engine.rb

    这里再解释一下……

    Rails Engine - Gems dependencies, how to load them into the application?

    【讨论】:

      猜你喜欢
      • 2012-12-25
      • 2012-08-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-04-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多