在葡萄API需要助手模块文件 - Require Helpers Module File in Grape API

- 此内容更新于:2016-02-01
主题:

这是我的葡萄API的结构。这是我的warden_helpers。rb文件;这就是我试图要求他们,如果我作为一个整体包括助手应用程序。rb或把模块在同一个文件中,一切工作正常。但是这种方法将抛出一个错误:

原文:

This is the structure of my Grape API.

app
|  /api
   |   /v1
   |   application.rb
   /helpers
   |   warden_helpers.rb
   /models
   |   user.rb

This is my warden_helpers.rb file;

require 'grape'
require 'warden'

module WardenHelpers
    extend Grape::API::Helpers
    # Helpers go here
end

And this is how I am trying to require them in application.rb;

require 'grape'

# Load files from folders
Dir["#{File.dirname(__FILE__)}/app/models/**/*.rb"].each { |f| require f }
Dir["#{File.dirname(__FILE__)}/app/api/**/*.rb"].each { |f| require f }
Dir["#{File.dirname(__FILE__)}/app/helpers/**/*.rb"].each { |f| require f }

module API
  class Root < Grape::API
    helpers WardenHelpers
  end
end

If I include the helpers as a block in application.rb or place the module in the same file - everything works fine. But this approach throws an error: in '<class:Root>': uninitialized constant API::Root::WardenHelpers (NameError)