1

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'

OK、それで、paperclip gem の has_attached_file メソッドが見つからないようです。なんで?わからない。

inherited_resources に移ると、scaffold で生成されたコントローラーを次のように変更しました。

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)

私の質問は、なぜこれらが機能しないのですか? エンジン、ネームスペース、および gem のロードについて、私がまったく気付いていない何かがあるというのが私の予感です。

4

1 に答える 1

1

エンジン内の各ジェムをイニシャライザ ファイルで要求する必要があります。lib/<your_engine_name>/engine.rb

ここでもう少し説明を...

Rails エンジン - Gem の依存関係、それらをアプリケーションにロードする方法は?

于 2011-06-28T22:15:31.297 に答える