簡體   English   中英

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

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

我正在使用sitemap_generator( https://github.com/kjvarga/sitemap_generator )gem,似乎我的代碼在開發環境中工作正常,但是當我嘗試在生產環境中運行它時,出現以下錯誤:

private method `split' called for nil:NilClass

這僅發生在生產環境中(在我的機器和生產服務器上),所有東西在開發中都運行良好。 這是我的站點地圖代碼,位於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

在生產環境中運行rake任務時,出現上述錯誤。 這是整個堆棧跟蹤:

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:

我找不到在生產和開發之間會導致這種情況的任何區別。 您能提供的任何幫助將不勝感激!

編輯:我可能應該補充一點,這似乎與生成路徑有問題。 刪除所有路徑可使此工作正常進行。 問題在於路徑正確,並且此代碼在開發中運行良好。 我不知道為什么在生產中不會生成路徑。

編輯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

這是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

產品和開發環境不共享數據庫。

這里的問題是,由於您是在請求上下文之外運行rake任務,因此沒有主機可對其進行操作。 從來源:

異常發生在: 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/url_rewriter.rb#L6

我們看到傳入的主機是

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

通常,將從請求的Host標頭填充default_url_options[:host] ,但是由於它在請求之外,因此沒有主機標頭可以操作。 您需要做的是:

  1. 通過覆蓋應用程序控制器中的default_url_options設置默認主機: http : //guides.rubyonrails.org/action_controller_overview.html#default-url-options
  2. 或者,將:host傳遞給您從調用motorsport_event_organization_path位置發送的路線選項。

第一種選擇可能是最簡單的。

如果您定義以下錯誤,該錯誤將消失:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM