我为Rails制作了一个宝石。我需要访问ApplicationController,因为我会玩它。绝对没有在线提供有关如何处理gemspec
的信息,然后以某种方式设法在我的gem中访问Rails。
我想目标最终是能够与Rails交谈,如:
module Rails
module ActionController
#code
end
end
答案 0 :(得分:2)
如果您正在为Rails专门开发一个gem,我强烈建议您使用rails plugin new gem_name生成初始支架。有关开发rails插件的大量信息。
生成的初始结构如下所示:
gem_name
gem_name.gemspec
lib/
gem_name.rb
gem_name/
version.rb
engine.rb # if generated using --mountable
整个rails环境变得可用[编辑:加载gem之后],因此可以像这样扩展ApplicationController
:
# lib/gem_name.rb
require 'gem_name/controller_extensions'
module GemName
end
# lib/gem_name/controller_extensions.rb
module GemName::ControllerExtensions
# bleh
end
# dummy_application/app/application_controller.rb
class ApplicationController < ActionController::Base
include GemName::ControllerExtensions
end
看看这个question。