1

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

由于未知原因,初始化程序中定义的配置细节在应用程序启动时已经消失。它是同一个对象(相同的值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 上发布了该项目的问题,因为我无法确定问题的根源,我必须假设它在 gem 中。任何有关如何进一步解决此问题的帮助都将非常受欢迎!

4

1 回答 1

1

解决方案是使用由于几个月前的更改而需要的未记录方法。

contentful_rails gem需要contentful_model(反之亦然), contentful_model 的唯一配置文档在该项目的 README 中,描述了我的问题中的方法。然后在初始化 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。

于 2015-07-24T18:33:32.813 回答