简体   繁体   English

私有方法`split'要求nil:NilClass

[英]private method `split' called for nil:NilClass

I'm using the sitemap_generator ( https://github.com/kjvarga/sitemap_generator ) gem and seem to have my code working fine in the development environment but when I try to run it in production I get the following error: 我正在使用sitemap_generator( https://github.com/kjvarga/sitemap_generator )gem,似乎我的代码在开发环境中工作正常,但是当我尝试在生产环境中运行它时,出现以下错误:

private method `split' called for nil:NilClass

This only happens in the production environment (both on my machine and my production server), everything runs fine in development. 这仅发生在生产环境中(在我的机器和生产服务器上),所有东西在开发中都运行良好。 Here's my site map code which resides in config/sitemap.rb: 这是我的站点地图代码,位于config / sitemap.rb中:

SitemapGenerator::Sitemap.default_host = "https://www.mysite.com"
SitemapGenerator::Sitemap.create do
  # The root path '/' and sitemap index file are added automatically for you.
  # Links are added to the Sitemap in the order they are specified.  

  add '/classifieds', :changefreq => 'yearly', :priority => 0.75, :lastmod => '2013-01-01'
  add '/sell_modified_cars', :changefreq => 'yearly', :priority => 0.75, :lastmod => '2013-01-01'
  add '/sell_race_cars', :changefreq => 'yearly', :priority => 0.75, :lastmod => '2013-01-01'

  add '/motorsport_event/organizations', :changefreq => 'monthly', :priority => 0.8, :lastmod => Organization.last.created_at

  Organization.find_each do |org|
    add motorsport_event_organization_path(org), :changefreq => 'daily', :priority => 0.9, :lastmod => Time.now
  end

  Event.find_each do |event|
    add motorsport_event_organization_event_path(event.organization, event), :changefreq => 'daily', :priority => 1, :lastmod => event.event_date
  end

  Listing.active.each do |listing|
    add classifieds_listing_path(listing), :changefreq => 'daily', :priority => 1, :lastmod => listing.updated_at
  end

  add '/faq', :changefreq => 'monthly', :priority => 0.5, :lastmod => '2013-07-01'
  add '/how_it_works_for_buyers', :changefreq => 'yearly', :priority => 0.5, :lastmod => '2013-01-01'
  add '/how_it_works_for_sellers', :changefreq => 'yearly', :priority => 0.5, :lastmod => '2013-01-01'
  add '/buyer_faq', :changefreq => 'yearly', :priority => 0.5, :lastmod => '2013-01-01'
  add '/seller_faq', :changefreq => 'yearly', :priority => 0.5, :lastmod => '2013-01-01'
  add '/search', :changefreq => 'yearly', :priority => 0.5, :lastmod => '2013-01-01'


  ############
  # PARTNERS #
  ############

  add '/', :changefreq => 'monthly', :priority => 0.9, :lastmod => '2013-07-01', :host => 'https://partners.mysite.com'
  add '/blogs', :changefreq => 'monthly', :priority => 0.8, :lastmod => Blog.last.created_at, :host => 'https://partners.mysite.com'

  Blog.find_each do |blog|
    add partners_blog_path(blog), :priority => 1, :lastmod => Blog.last.created_at, :host => 'https://partners.mysite.com'
  end

  add '/pricing', :changefreq => 'monthly', :priority => 0.8, :lastmod => '2013-07-01', :host => 'https://partners.mysite.com'
  add '/faq', :changefreq => 'monthly', :priority => 0.5, :lastmod => '2013-07-01', :host => 'https://partners.mysite.com'
  add '/whoweare', :changefreq => 'yearly', :priority => 0.5, :lastmod => '2012-01-01', :host => 'https://partners.mysite.com'
  add '/workwithus', :changefreq => 'monthly', :priority => 0.5, :lastmod => '2012-01-01', :host => 'https://partners.mysite.com'
  add '/contactus', :priority => 0.5, :lastmod => '2012-01-01', :host => 'https://partners.mysite.com'

end

When I run the rake task in the production environment I get the error noted above. 在生产环境中运行rake任务时,出现上述错误。 Here's the whole stack trace: 这是整个堆栈跟踪:

rake sitemap:refresh --trace

** Invoke sitemap:refresh (first_time)
** Invoke sitemap:create (first_time)
** Invoke sitemap:require_environment (first_time)
** Execute sitemap:require_environment
** Invoke environment (first_time)
** Execute environment
** Execute sitemap:create

rake aborted!
private method `split' called for nil:NilClass
/shared/bundled_gems/ruby/1.8/gems/subdomain-fu-0.5.4/lib/subdomain-fu.rb:65:in `host_without_subdomain'
/shared/bundled_gems/ruby/1.8/gems/subdomain-fu-0.5.4/lib/subdomain-fu.rb:85:in `change_subdomain_of_host'
/shared/bundled_gems/ruby/1.8/gems/subdomain-fu-0.5.4/lib/subdomain-fu.rb:72:in `rewrite_host_for_subdomains'
/shared/bundled_gems/ruby/1.8/gems/subdomain-fu-0.5.4/lib/subdomain_fu/url_rewriter.rb:6:in `url_for'
(eval):17:in `motorsport_event_organization_path'
/releases/20130720225723/config/sitemap.rb:13:in `run'
/shared/bundled_gems/ruby/1.8/gems/activerecord-2.3.16/lib/active_record/batches.rb:26:in `find_each'
/shared/bundled_gems/ruby/1.8/gems/activerecord-2.3.16/lib/active_record/batches.rb:26:in `each'
/shared/bundled_gems/ruby/1.8/gems/activerecord-2.3.16/lib/active_record/batches.rb:26:in `find_each'
/shared/bundled_gems/ruby/1.8/gems/activerecord-2.3.16/lib/active_record/batches.rb:66:in `find_in_batches'
/shared/bundled_gems/ruby/1.8/gems/activerecord-2.3.16/lib/active_record/batches.rb:25:in `find_each'
/releases/20130720225723/config/sitemap.rb:12:in `run'
/shared/bundled_gems/ruby/1.8/gems/sitemap_generator-4.1.1/lib/sitemap_generator/interpreter.rb:59:in `instance_eval'
/shared/bundled_gems/ruby/1.8/gems/sitemap_generator-4.1.1/lib/sitemap_generator/interpreter.rb:59:in `eval'
/shared/bundled_gems/ruby/1.8/gems/sitemap_generator-4.1.1/lib/sitemap_generator/link_set.rb:39:in `create'
/shared/bundled_gems/ruby/1.8/gems/sitemap_generator-4.1.1/lib/sitemap_generator.rb:33:in `send'
/shared/bundled_gems/ruby/1.8/gems/sitemap_generator-4.1.1/lib/sitemap_generator.rb:33:in `method_missing'
/releases/20130720225723/config/sitemap.rb:2:in `run'
/shared/bundled_gems/ruby/1.8/gems/sitemap_generator-4.1.1/lib/sitemap_generator/tasks.rb:51
/shared/bundled_gems/ruby/1.8/gems/rake-0.8.7/lib/rake.rb:636:in `call'
/shared/bundled_gems/ruby/1.8/gems/rake-0.8.7/lib/rake.rb:636:in `execute'
/shared/bundled_gems/ruby/1.8/gems/rake-0.8.7/lib/rake.rb:631:in `each'
/shared/bundled_gems/ruby/1.8/gems/rake-0.8.7/lib/rake.rb:631:in `execute'
/shared/bundled_gems/ruby/1.8/gems/rake-0.8.7/lib/rake.rb:597:in `invoke_with_call_chain'
/usr/lib/ruby/1.8/monitor.rb:242:in `synchronize'
/shared/bundled_gems/ruby/1.8/gems/rake-0.8.7/lib/rake.rb:590:in `invoke_with_call_chain'
/shared/bundled_gems/ruby/1.8/gems/rake-0.8.7/lib/rake.rb:607:in `invoke_prerequisites'
/shared/bundled_gems/ruby/1.8/gems/rake-0.8.7/lib/rake.rb:604:in `each'
/shared/bundled_gems/ruby/1.8/gems/rake-0.8.7/lib/rake.rb:604:in `invoke_prerequisites'
/shared/bundled_gems/ruby/1.8/gems/rake-0.8.7/lib/rake.rb:596:in `invoke_with_call_chain'
/usr/lib/ruby/1.8/monitor.rb:242:in `synchronize'
/shared/bundled_gems/ruby/1.8/gems/rake-0.8.7/lib/rake.rb:590:in `invoke_with_call_chain'
/shared/bundled_gems/ruby/1.8/gems/rake-0.8.7/lib/rake.rb:583:in `invoke'
/shared/bundled_gems/ruby/1.8/gems/rake-0.8.7/lib/rake.rb:2051:in `invoke_task'
/shared/bundled_gems/ruby/1.8/gems/rake-0.8.7/lib/rake.rb:2029:in `top_level'
/shared/bundled_gems/ruby/1.8/gems/rake-0.8.7/lib/rake.rb:2029:in `each'
/shared/bundled_gems/ruby/1.8/gems/rake-0.8.7/lib/rake.rb:2029:in `top_level'
/shared/bundled_gems/ruby/1.8/gems/rake-0.8.7/lib/rake.rb:2068:in `standard_exception_handling'
/shared/bundled_gems/ruby/1.8/gems/rake-0.8.7/lib/rake.rb:2023:in `top_level'
/shared/bundled_gems/ruby/1.8/gems/rake-0.8.7/lib/rake.rb:2001:in `run'
/shared/bundled_gems/ruby/1.8/gems/rake-0.8.7/lib/rake.rb:2068:in `standard_exception_handling'
/shared/bundled_gems/ruby/1.8/gems/rake-0.8.7/lib/rake.rb:1998:in `run'
/shared/bundled_gems/ruby/1.8/gems/rake-0.8.7/bin/rake:31
/shared/bundled_gems/ruby/1.8/bin/rake:19:

I can't find anything different between production and development that would cause this. 我找不到在生产和开发之间会导致这种情况的任何区别。 Any help you could provide would be appreciated! 您能提供的任何帮助将不胜感激!

EDIT: I should probably add that this seems to be something wrong with generating the paths. 编辑:我可能应该补充一点,这似乎与生成路径有问题。 Removing all of the paths makes this work fine. 删除所有路径可使此工作正常进行。 The problem is that the paths are correct and this code runs fine in development. 问题在于路径正确,并且此代码在开发中运行良好。 I don't know why the paths don't generate in production. 我不知道为什么在生产中不会生成路径。

EDIT 2: Here's development.rb: 编辑2:这是development.rb:

require 'rack/ssl-enforcer'
require 'stripe'

config.middleware.use ::Rack::SslEnforcer, :except => /^\/api\//

# Settings specified here will take precedence over those in config/environment.rb

# In the development environment your application's code is reloaded on
# every request.  This slows down response time but is perfect for development
# since you don't have to restart the webserver when you make code changes.
config.cache_classes = false

# Log error messages when you accidentally call methods on nil.
config.whiny_nils = true

# Show full error reports and disable caching
config.action_controller.consider_all_requests_local = true
config.action_view.debug_rjs                         = true
config.action_controller.perform_caching             = false

# Don't care if the mailer can't send
config.action_mailer.raise_delivery_errors = false

config.after_initialize do
  ActionMailer::Base.delivery_method = :amazon_ses
  ActionMailer::Base.custom_amazon_ses_mailer = AWS::SES::Base.new(:secret_access_key => S3_CONFIG[:secret_access_key], :access_key_id => S3_CONFIG[:access_key_id])
end

# Default APP Domain
APP_DOMAIN = 'mysite.dev'
ActionMailer::Base.default_url_options[:host] = APP_DOMAIN

# ensure the sessions work across the subdomains
begin
  config.action_controller.session[:domain] = ".#{APP_DOMAIN}"
rescue
  config.action_controller.session = {:domain => ".#{APP_DOMAIN}"}
end

Here's production.rb: 这是production.rb:

# Settings specified here will take precedence over those in config/environment.rb
require 'rack/ssl-enforcer'
require 'stripe'

config.middleware.use ::Rack::SslEnforcer, :except => /^\/api\//

# The production environment is meant for finished, "live" apps.
# Code is not reloaded between requests
config.cache_classes = true

# Full error reports are disabled and caching is turned on
config.action_controller.consider_all_requests_local = false
config.action_controller.perform_caching             = true
config.action_view.cache_template_loading            = true

# See everything in the log (default is :info)
# config.log_level = :debug

# Use a different logger for distributed setups
# config.logger = SyslogLogger.new

# Use a different cache store in production
# config.cache_store = :mem_cache_store

# Enable serving of images, stylesheets, and javascripts from an asset server
# config.action_controller.asset_host = "http://assets.example.com"

# Disable delivery errors, bad email addresses will be ignored
config.action_mailer.raise_delivery_errors = false

# Enable threaded mode
# config.threadsafe!

# Default APP Domain
APP_DOMAIN = 'mysite.com'
ActionMailer::Base.default_url_options[:host] = "www.#{APP_DOMAIN}"

# Amazon SES Mailer
config.after_initialize do
  ActionMailer::Base.delivery_method = :amazon_ses
  ActionMailer::Base.custom_amazon_ses_mailer = AWS::SES::Base.new(:secret_access_key => S3_CONFIG[:secret_access_key], :access_key_id => S3_CONFIG[:access_key_id])
end

# ensure the sessions work across the subdomains
begin
  config.action_controller.session[:domain] = ".#{APP_DOMAIN}"
rescue
  config.action_controller.session = {:domain => ".#{APP_DOMAIN}"}
end

The prod and dev environments do not share a DB. 产品和开发环境不共享数据库。

The issue here is that since you're running the rake task outside of the context of a request, there is no host for it to operate on. 这里的问题是,由于您是在请求上下文之外运行rake任务,因此没有主机可对其进行操作。 From the source: 从来源:

The exception occurs at: https://github.com/mbleigh/subdomain-fu/blob/v0.5.4/lib/subdomain-fu.rb#L65 异常发生在: https : //github.com/mbleigh/subdomain-fu/blob/v0.5.4/lib/subdomain-fu.rb#L65

We can see that it's trying to split a nil host. 我们可以看到它正在尝试拆分零个主机。 Following the stack trace to: https://github.com/mbleigh/subdomain-fu/blob/v0.5.4/lib/subdomain_fu/url_rewriter.rb#L6 遵循堆栈跟踪至: https : //github.com/mbleigh/subdomain-fu/blob/v0.5.4/lib/subdomain_fu/url_rewriter.rb#L6

We see that the host being passed in is 我们看到传入的主机是

options[:host] || default_url_options[:host]

Normally, default_url_options[:host] would be populated from the request's Host header, but since this is outside of a request, there's no host header to operate on. 通常,将从请求的Host标头填充default_url_options[:host] ,但是由于它在请求之外,因此没有主机标头可以操作。 What you need to do is either: 您需要做的是:

  1. Set a default host via overriding default_url_options in your application controller: http://guides.rubyonrails.org/action_controller_overview.html#default-url-options 通过覆盖应用程序控制器中的default_url_options设置默认主机: http : //guides.rubyonrails.org/action_controller_overview.html#default-url-options
  2. Or, pass in :host to the route options from where you're calling motorsport_event_organization_path . 或者,将:host传递给您从调用motorsport_event_organization_path位置发送的路线选项。

The first option is probably the easiest. 第一种选择可能是最简单的。

The error will go away if you define: 如果您定义以下错误,该错误将消失:

def nil.split
  tap { warn "Stand back, splitting nil!" }
end

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 私有方法`split'调用了nil:NilClass(NoMethodError) - private method `split' called for nil:NilClass (NoMethodError) 私有方法`select'调用了nil:NilClass(NoMethodError) - private method `select' called for nil:NilClass (NoMethodError) 私有方法“ chomp”调用了nil:NilClass(NoMethodError) - private method `chomp' called for nil:NilClass (NoMethodError) 错误(为nil:NilClass调用了私有方法“ select”): - Error (private method `select' called for nil:NilClass): 私有方法`puts'调用nil:NilClass(NoMethodError) - private method `puts' called for nil:NilClass (NoMethodError) pik错误:私有方法`gsub'调用nil:NilClass - pik Error: private method `gsub' called for nil:NilClass 私有方法“ main_image”是否需要nil:NilClass? 怎么修 - private method `main_image' called for nil:NilClass? how to fix 是什么导致在Rails迁移期间私有方法`gsub`调用nil:NilClass? - What is causing private method `gsub` called for nil:NilClass during Rails migration? Ruby 2.1:组成零件自行车->调用nil:NilClass的私有方法“ select”(NoMethodError) - Ruby 2.1: Composing a Bicycle of Parts -> private method `select' called for nil:NilClass (NoMethodError) Pik安装Ruby Yields:错误:私有方法'gsub'调用nil:NilClass - Pik Install Ruby Yields: Error: Private method 'gsub' called for nil:NilClass
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM