1

我目前正在构建一个rails引擎,在我的gemspec文件中我有这个gem meta-tags,我已经像这样添加了

保险.gemspec

$:.push File.expand_path("../lib", __FILE__)

# Maintain your gem's version:
require "insurance/version"

# Describe your gem and declare its dependencies:
Gem::Specification.new do |spec|
  spec.name        = "insurance"
  spec.version     = Insurance::VERSION
  spec.authors     = ["Loanstreet Tech"]
  spec.email       = ["techgroup@finology.com.my"]
  spec.homepage    = "https://github.com/loanstreet/insurance_loanstreet_plugin"
  spec.summary     = "Insurance Loanstreet."
  spec.description = "Loanstreet Insurance."
  spec.license     = "MIT"

  spec.files = Dir["{app,config,db,lib}/**/*", "MIT-LICENSE", "Rakefile", "README.md"]

  spec.add_dependency "rails", "~> 5.0.1"

  spec.add_dependency "pg", "0.18"
  spec.add_dependency "meta-tags"

end

我还添加了一个文件来将这个 gem 的初始化配置复制到我的主应用程序中,就像这样

在 lib/generators/insurance/install_generator.rb

module Insurance
  class InstallGenerator < Rails::Generators::Base
    source_root File.expand_path("../../templates", __FILE__)

    def copy_initializer
      template 'meta_tags.rb', 'config/initializers/meta_tags.rb'
    end
  end
end

在我的 main_application 中,当我运行时,会像这样创建一个 config/initializers/meta_tags.rb 文件rails g insurance:install

配置/初始化程序/meta_tags.rb

MetaTags.configure do |config|
  # How many characters should the title meta tag have at most. Default is 70.
  # Set to nil or 0 to remove limits.
  config.title_limit = 140

  # When true, site title will be truncated instead of title. Default is false.
  # config.truncate_site_title_first = false

  # Maximum length of the page description. Default is 300.
  # Set to nil or 0 to remove limits.
  config.description_limit = 500

  # Maximum length of the keywords meta tag. Default is 255.
  # config.keywords_limit = 255

  # Default separator for keywords meta tag (used when an Array passed with
  # the list of keywords). Default is ", ".
  # config.keywords_separator = ', '

  # When true, keywords will be converted to lowercase, otherwise they will
  # appear on the page as is. Default is true.
  # config.keywords_lowercase = true

  # When false, generated meta tags will be self-closing (<meta ... />) instead
  # of open (`<meta ...>`). Default is true.
  # config.open_meta_tags = true

  # List of additional meta tags that should use "property" attribute instead
  # of "name" attribute in <meta> tags.
  # config.property_tags.push(
  #   'x-hearthstone:deck',
  # )
end

但是当我运行 rails s 时,我得到错误config/initializers/meta_tags.rb:2:in“:未初始化的常量 MetaTags (NameError)”。

那么我应该如何在我的 Rails 引擎中添加一个 gem 并能够从我的 rails 引擎或我的主应用程序调用常量。

任何建议表示赞赏

4

0 回答 0