【问题标题】:Using Rack gem in Rails在 Rails 中使用 Rack gem
【发布时间】:2013-10-04 04:13:44
【问题描述】:

我正在尝试使用我刚刚在 Rails 项目中编写的一个愚蠢的小 Rack gem,但每当我调用它时,我都会不断收到 uninitialized constant 错误。这让我发疯了,因为我之前有 already written Rack gems,并且 那些 在我的 Rails 项目中工作得很好。

在我的 Gemfile 中:

gem 'hide_heroku', :git => 'https://github.com/ykessler/hide-heroku'

在我的 application.rb 中:

module Tester
  class Application < Rails::Application

    config.middleware.use Rack::HideHeroku

但是启动我的本地服务器,我得到:

未初始化的常量 Rack::HideHeroku (NameError)

宝石:

module Rack

  class HideHeroku

    def initialize(app)
      @app=app
    end

    def call(env)
      @status, @headers, @response = @app.call(env)
      @request = Rack::Request.new(env)
      [@status, _apply_headers, @response]
    end

    private

      def _apply_headers
        if /\.herokuapp\.com\/?.*/ =~ @request.url
          @headers['X-Robots-Tag'] = 'noindex, nofollow'
        end
        @headers
      end

  end

end

在这里查看:https://github.com/ykessler/hide-heroku

【问题讨论】:

    标签: ruby-on-rails ruby gem rack


    【解决方案1】:

    这里的问题可能是项目的结构。你需要在你的 gem 中有一个 lib/hide_heroku.rb 文件,它引导 gem,或者你需要在你的 gem 行上指定 require 路径,如下所示:

    gem 'hide_heroku', :git => 'https://github.com/ykessler/hide-heroku', :require => "rack/hide_heroku"
    

    (或者你可以在你的应用程序中直接require 'rack/hide_heroku')。

    当您调用 Bundler.setup 时,Bundler 会尝试要求 gem 的名称,但如果您不使用该文件结构,它会找不到要包含的文件。

    【讨论】:

    • 克里斯-非常感谢-就是这样
    猜你喜欢
    • 2013-06-06
    • 2012-05-11
    • 1970-01-01
    • 2011-05-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-08
    相关资源
    最近更新 更多