【问题标题】:Configuration of ContentfulModel gem is lost after initializationContentfulModel gem 的配置在初始化后丢失
【发布时间】:2015-07-25 02:33:32
【问题描述】:

全新的 Rails 4.2.3 应用程序。 Gemfile 的唯一更改是删除了 Spring,添加了 dotenv,以及 ruby​​gems.org 上发布的最新的 contentful_railscontentful_model gems。

由于未知原因,初始化程序中定义的配置细节在应用启动时已经消失。它是同一个对象(ContentfulModel.configuration.object_id 的值相同),但以前正确的值现在是 nil

我添加了一个初始化器,如 README 中所示。

$ cat config/initializers/contentful_model.rb 
ContentfulModel.configure do |config|
  byebug
  config.access_token         = ENV['CONTENTFUL_ACCESS_TOKEN']
  config.preview_access_token = ENV['CONTENTFUL_PREVIEW_ACCESS_TOKEN']
  config.space                = ENV['CONTENTFUL_SPACE']
#  config.options              = {
    #extra options to send to the Contentful::Client
#  }
end

我定义了一个模型,类别。

$ cat app/models/category.rb 
class Category < ContentfulModel::Base
   self.content_type_id = "[category content type string]"
end

所以当我启动 Rails 控制台时会发生以下情况:

$ rails c

[1, 9] in /home/trevor/code/chef/www-contentful-rails/config/initializers/contentful_model.rb
   1: ContentfulModel.configure do |config|
   2:   config.access_token         = ENV['CONTENTFUL_ACCESS_TOKEN']
   3:   config.preview_access_token = ENV['CONTENTFUL_PREVIEW_ACCESS_TOKEN']
   4:   config.space                = ENV['CONTENTFUL_SPACE']
   5: #  config.options              = {
   6:     #extra options to send to the Contentful::Client
   7: #  }
   8:   byebug
=> 9: end
(byebug) ContentfulModel.configuration
#<ContentfulModel::Configuration:0x00000005bc7be0 @access_token="[my actual token string]", @entry_mapping={}, @preview_access_token="[my actual preview token string]", @space="[my actual space]">
(byebug) continue
/home/trevor/.rvm/gems/ruby-2.2.2@www-contentful-rails/gems/actionpack-4.2.3/lib/action_dispatch/http/mime_type.rb:163: warning: already initialized constant Mime::JSON                    
/home/trevor/.rvm/gems/ruby-2.2.2@www-contentful-rails/gems/actionpack-4.2.3/lib/action_dispatch/http/mime_type.rb:163: warning: previous definition of JSON was here                       
Loading development environment (Rails 4.2.3)
2.2.2 :001 > ContentfulModel.configuration
 => #<ContentfulModel::Configuration:0x00000005bc7be0 @access_token=nil, @entry_mapping={"[category content type string]"=>Category}, @preview_access_token=nil, @space=nil>                        
2.2.2 :002 > 

我花了很多时间筛选 gem 源代码并单步调试调试器,但没有任何结果。我已经在 GitHub 上为该项目发布了 an issue,因为我无法确定问题的根源,我不得不假设它在 gem 中。非常欢迎任何有关如何进一步解决此问题的帮助!

【问题讨论】:

    标签: ruby-on-rails ruby contentful


    【解决方案1】:

    solution 是使用由于changes a few months ago 而需要的未记录方法。

    contentful_rails gem 需要 contentful_model(反之亦然),contentful_model 的唯一配置文档在该项目的自述文件中,描述了我的问题中的方法。然后在初始化 contentful_rails 时完全擦除以这种方式进行的配置,这期望配置在其自己的初始化程序中完成。

    所以我删除了 config/initializers/contentful_model.rb,现在我的 config/initializers/contents_rails.rb 文件看起来像:

    ContentfulRails.configure do |config|
      config.authenticate_webhooks = true # false here would allow the webhooks to process without basic auth
      config.webhooks_username     = ENV['CONTENTFUL_WEBHOOK_USERNAME']
      config.webhooks_password     = ENV['CONTENTFUL_WEBHOOK_PASSWORD']
    
      config.access_token          = ENV['CONTENTFUL_ACCESS_TOKEN']
      config.preview_access_token  = ENV['CONTENTFUL_PREVIEW_ACCESS_TOKEN']
      config.space                 = ENV['CONTENTFUL_SPACE']
      config.contentful_options    = {
        #extra options to send to the Contentful::Client
      }
    end
    

    值得注意的是 config.options 不再是一个东西;它是 config.contentful_options。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-08-21
      • 2011-09-20
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多