【问题标题】:When mounting Rails Engine, module's name is not recognized安装 Rails Engine 时,模块名称无法识别
【发布时间】:2019-06-12 19:50:44
【问题描述】:

我有一个带有模块 GithubWebhooks 的 Rails 引擎 github_webhooks。

在引擎中,我有:

module GithubWebhooks
  class EventController < ApplicationController
    require 'octokit'
    client = Octokit::Client.new(GithubWebhooks.access_token)

    def destroy
       ...code here...
    end

  end
end

# pullrequest_controller.rb

在 lib/github_webhooks.rb 中,

require "github_webhooks/engine"

module GithubWebhooks
  mattr_accessor :access_token 
  mattr_accessor :base_url
end

# github_webhooks.rb

Rails 应用程序中的/config 下应该有一个初始化文件,它允许用户输入他们的 OAuth 访问令牌 (:access_token) 和一个指向 POST 的基本 URL 到 (:base_url)。

当我尝试将 Rails 引擎安装到我的应用程序 (sample_app) 时,我会执行以下操作,我认为这是标准程序:

我将这一行放在我的 gemfile 中:

gem 'github_webhooks', git: 'https://github.com/path_to/my_repo'

# Gemfile

然后在路线中,我写道:

mount GithubWebhooks::Engine, at: "/github_webhooks"

# routes.rb

我已经弄清楚了如何使用环境变量,方法是将令牌放在 .yml 文件中,然后使用 gitignore 安全地允许用户将更改提交到他们自己的应用程序,而无需在提交中包含他们的令牌。我能够验证通过使用 ENV[],我可以根据需要获取令牌。所以这不是真正的问题。 这让我想到了真正的问题。

Rails 似乎无法识别我的引擎的名称,因为路由文件中的行似乎是导致问题的原因。它给出了错误

/home/mschneider25/.rvm/gems/ruby-2.6.1/gems/bootsnap-1.4.4/lib/bootsnap/load_path_cache/core_ext/active_support.rb:79:in `block in load_missing_constant': 未初始化的常量GithubWebhooks (NameError)

我不太确定为什么它说 GithubWebhooks 未初始化;这是引擎中模块的名称。 我遗漏了什么,这会让 Rails 识别我的引擎和其中的模块?

【问题讨论】:

  • 你是不是做了bundle install然后重启rails s
  • 另外,你在sample_app 中使用了require 引擎吗?
  • @jvillian 我用什么语法来要求引擎,在什么文件中?

标签: ruby-on-rails ruby rubygems rails-engines


【解决方案1】:

尝试通过放置来要求宝石:

require 'github_webhooks'

config/application.rb。或者,

gem 'github_webhooks', git: 'https://github.com/path_to/my_repo', require: 'github_webhooks'

在你的Gemfile

【讨论】:

    猜你喜欢
    • 2015-09-22
    • 2021-11-19
    • 1970-01-01
    • 2023-02-07
    • 2018-09-23
    • 2019-06-02
    • 2017-05-16
    相关资源
    最近更新 更多