2016-12-21 2 views
0

'app/helpers/transaction_helper.rb'フォルダからモジュールを使用しようとしています。 (これは、アプリケーションがapi_onlyフラグを使用して、ヘルパーがデフォルトで生成またはロードされていないことに注意してください。)Rails 5 API - api_onlyフラグを使用したときのヘルパーの自動読み込みエラー

module TransactionHelper 
    module Generator 
    def self.code_alphanumeric(params) 
    end 
    end 
end 

私はこのエラーを得た:私が追加しようとしました

NameError: uninitialized constant TransactionHelper 

をapplication.rbに次の行

config.autoload_paths + =%のW(#{config.root} /アプリ/ヘルパー)

require_relative 'boot' 

require "rails" 
# Pick the frameworks you want: 
require "active_model/railtie" 
require "active_job/railtie" 
require "active_record/railtie" 
require "action_controller/railtie" 
require "action_mailer/railtie" 
require "action_view/railtie" 
require "action_cable/engine" 
# require "sprockets/railtie" 
require "rails/test_unit/railtie" 

# Require the gems listed in Gemfile, including any gems 
# you've limited to :test, :development, or :production. 
Bundler.require(*Rails.groups) 

module SistemaControleContasApi 
    class Application < Rails::Application 
    config.autoload_paths += %W(#{config.root}/app/helpers) 
    # Settings in config/environments/* take precedence over those specified here. 
    # Application configuration should go into files in config/initializers 
    # -- all .rb files in that directory are automatically loaded. 

    # Only loads a smaller set of middleware suitable for API only apps. 
    # Middleware like session, flash, cookies can be added back manually. 
    # Skip views, helpers and assets when generating a new resource. 
    config.api_only = true 
    end 
end 

しかし、これは動作しません。このファイルをapp/modelsディレクトリに配置しようとすると、この作業が行われます。しかし、これはヘルパーを配置するための適切なローカルではありません。

誰かが助けてくれますか?

+0

_API mode_との戦いの代わりに、そのコードを/ lib/transaction_helper/generatorに移動して、そのディレクトリを 'autoload_paths'に追加してみてください。 – pdoherty926

+0

'config.autoload_paths'を' config.eager_load_paths'に置き換えることができますか? – 31piy

答えて

0

通常、Rails™の命名規則に従うと、app以下のものはすべてデフォルトでオートロードされます。ここをクリックDocsを参照してください。

set_autoload_paths: This initializer runs before bootstrap_hook. Adds all sub-directories of app and paths specified by config.autoload_paths, config.eager_load_paths and config.autoload_once_paths to ActiveSupport::Dependencies.autoload_paths.

最初の手順では、設定からconfig.autoload_paths += %W(#{config.root}/app/helpers)を削除する必要があります。

次に、モジュールごとにサブフォルダを作成する必要があります。
は、だからあなたの問題のために、あなたはまた、あなたがモジュールスコープでselfを必要としないapp/modules/transaction_helper/generator.rb

の下にモジュールを配置する必要があります。


個人的に私は次のようにヘルパーを作成します。

module TransactionHelper 
    def generate_alphanumeric_code 
    # params are automatically available in helpers. 
    end 
end 
をそして app/modules/transaction_helper.rb下に置きます。

関連する問題