Rails - 在"private"中:nil is not a symbo l (TypeError)



它出现在生产服务器中,而在本地服务器中没有出现,问题在Subscribe_Controller中,在那里我定义了发送给MailChimp的邮件列表的参数,这里是$heroku run rails console的错误日志。

/app/app/controllers/subscribe_controller.rb:11:in `private': nil is not a symbo
l (TypeError)
        from /app/app/controllers/subscribe_controller.rb:11:in `<class:Subscrib
eController>'
        from /app/app/controllers/subscribe_controller.rb:1:in `<top (required)>
'
        from /app/vendor/bundle/ruby/2.0.0/gems/activesupport-4.2.7.1/lib/active
_support/dependencies.rb:274:in `require'
        from /app/vendor/bundle/ruby/2.0.0/gems/activesupport-4.2.7.1/lib/active
_support/dependencies.rb:274:in `block in require'
        from /app/vendor/bundle/ruby/2.0.0/gems/activesupport-4.2.7.1/lib/active
_support/dependencies.rb:240:in `load_dependency'
        from /app/vendor/bundle/ruby/2.0.0/gems/activesupport-4.2.7.1/lib/active
_support/dependencies.rb:274:in `require'
        from /app/vendor/bundle/ruby/2.0.0/gems/activesupport-4.2.7.1/lib/active
_support/dependencies.rb:360:in `require_or_load'
        from /app/vendor/bundle/ruby/2.0.0/gems/activesupport-4.2.7.1/lib/active
_support/dependencies.rb:317:in `depend_on'
        from /app/vendor/bundle/ruby/2.0.0/gems/activesupport-4.2.7.1/lib/active
_support/dependencies.rb:233:in `require_dependency'
        from /app/vendor/bundle/ruby/2.0.0/gems/railties-4.2.7.1/lib/rails/engin
e.rb:472:in `block (2 levels) in eager_load!'
        from /app/vendor/bundle/ruby/2.0.0/gems/railties-4.2.7.1/lib/rails/engin
e.rb:471:in `each'
        from /app/vendor/bundle/ruby/2.0.0/gems/railties-4.2.7.1/lib/rails/engin
e.rb:471:in `block in eager_load!'
        from /app/vendor/bundle/ruby/2.0.0/gems/railties-4.2.7.1/lib/rails/engin
e.rb:469:in `each'
        from /app/vendor/bundle/ruby/2.0.0/gems/railties-4.2.7.1/lib/rails/engin
e.rb:469:in `eager_load!'
        from /app/vendor/bundle/ruby/2.0.0/gems/railties-4.2.7.1/lib/rails/engin
e.rb:346:in `eager_load!'
        from /app/vendor/bundle/ruby/2.0.0/gems/railties-4.2.7.1/lib/rails/appli
cation/finisher.rb:56:in `each'
        from /app/vendor/bundle/ruby/2.0.0/gems/railties-4.2.7.1/lib/rails/appli
cation/finisher.rb:56:in `block in <module:Finisher>'
        from /app/vendor/bundle/ruby/2.0.0/gems/railties-4.2.7.1/lib/rails/initi
alizable.rb:30:in `instance_exec'
        from /app/vendor/bundle/ruby/2.0.0/gems/railties-4.2.7.1/lib/rails/initi
alizable.rb:30:in `run'
        from /app/vendor/bundle/ruby/2.0.0/gems/railties-4.2.7.1/lib/rails/initi
alizable.rb:55:in `block in run_initializers'
        from /app/vendor/ruby-2.0.0/lib/ruby/2.0.0/tsort.rb:150:in `block in tso
rt_each'
        from /app/vendor/ruby-2.0.0/lib/ruby/2.0.0/tsort.rb:183:in `block (2 lev
els) in each_strongly_connected_component'
        from /app/vendor/ruby-2.0.0/lib/ruby/2.0.0/tsort.rb:210:in `block (2 lev
els) in each_strongly_connected_component_from'
        from /app/vendor/ruby-2.0.0/lib/ruby/2.0.0/tsort.rb:210:in `block (2 lev
els) in each_strongly_connected_component_from'
        from /app/vendor/ruby-2.0.0/lib/ruby/2.0.0/tsort.rb:210:in `block (2 lev
els) in each_strongly_connected_component_from'
        from /app/vendor/ruby-2.0.0/lib/ruby/2.0.0/tsort.rb:219:in `each_strongl
y_connected_component_from'
        from /app/vendor/ruby-2.0.0/lib/ruby/2.0.0/tsort.rb:209:in `block in eac
h_strongly_connected_component_from'
        from /app/vendor/bundle/ruby/2.0.0/gems/railties-4.2.7.1/lib/rails/initi
alizable.rb:44:in `each'
        from /app/vendor/bundle/ruby/2.0.0/gems/railties-4.2.7.1/lib/rails/initi
alizable.rb:44:in `tsort_each_child'
        from /app/vendor/ruby-2.0.0/lib/ruby/2.0.0/tsort.rb:203:in `each_strongl
y_connected_component_from'
        from /app/vendor/ruby-2.0.0/lib/ruby/2.0.0/tsort.rb:209:in `block in eac
h_strongly_connected_component_from'
        from /app/vendor/bundle/ruby/2.0.0/gems/railties-4.2.7.1/lib/rails/initi
alizable.rb:44:in `each'
        from /app/vendor/bundle/ruby/2.0.0/gems/railties-4.2.7.1/lib/rails/initi
alizable.rb:44:in `tsort_each_child'
        from /app/vendor/ruby-2.0.0/lib/ruby/2.0.0/tsort.rb:203:in `each_strongl
y_connected_component_from'
        from /app/vendor/ruby-2.0.0/lib/ruby/2.0.0/tsort.rb:209:in `block in eac
h_strongly_connected_component_from'
        from /app/vendor/bundle/ruby/2.0.0/gems/railties-4.2.7.1/lib/rails/initi
alizable.rb:44:in `each'
        from /app/vendor/bundle/ruby/2.0.0/gems/railties-4.2.7.1/lib/rails/initi
alizable.rb:44:in `tsort_each_child'
        from /app/vendor/ruby-2.0.0/lib/ruby/2.0.0/tsort.rb:203:in `each_strongl
y_connected_component_from'
        from /app/vendor/ruby-2.0.0/lib/ruby/2.0.0/tsort.rb:182:in `block in eac
h_strongly_connected_component'
        from /app/vendor/ruby-2.0.0/lib/ruby/2.0.0/tsort.rb:180:in `each'
        from /app/vendor/ruby-2.0.0/lib/ruby/2.0.0/tsort.rb:180:in `each_strongl
y_connected_component'
        from /app/vendor/ruby-2.0.0/lib/ruby/2.0.0/tsort.rb:148:in `tsort_each'
        from /app/vendor/bundle/ruby/2.0.0/gems/railties-4.2.7.1/lib/rails/initi
alizable.rb:54:in `run_initializers'
        from /app/vendor/bundle/ruby/2.0.0/gems/railties-4.2.7.1/lib/rails/appli
cation.rb:352:in `initialize!'
        from /app/config/environment.rb:5:in `<top (required)>'
        from /app/vendor/bundle/ruby/2.0.0/gems/activesupport-4.2.7.1/lib/active
_support/dependencies.rb:274:in `require'
        from /app/vendor/bundle/ruby/2.0.0/gems/activesupport-4.2.7.1/lib/active
_support/dependencies.rb:274:in `block in require'
        from /app/vendor/bundle/ruby/2.0.0/gems/activesupport-4.2.7.1/lib/active
_support/dependencies.rb:240:in `load_dependency'
        from /app/vendor/bundle/ruby/2.0.0/gems/activesupport-4.2.7.1/lib/active
_support/dependencies.rb:274:in `require'
        from /app/vendor/bundle/ruby/2.0.0/gems/railties-4.2.7.1/lib/rails/appli
cation.rb:328:in `require_environment!'
        from /app/vendor/bundle/ruby/2.0.0/gems/railties-4.2.7.1/lib/rails/comma
nds/commands_tasks.rb:142:in `require_application_and_environment!'
        from /app/vendor/bundle/ruby/2.0.0/gems/railties-4.2.7.1/lib/rails/comma
nds/commands_tasks.rb:67:in `console'
        from /app/vendor/bundle/ruby/2.0.0/gems/railties-4.2.7.1/lib/rails/comma
nds/commands_tasks.rb:39:in `run_command!'
        from /app/vendor/bundle/ruby/2.0.0/gems/railties-4.2.7.1/lib/rails/comma
nds.rb:17:in `<top (required)>'
        from /app/bin/rails:4:in `require'
        from /app/bin/rails:4:in `<main>'

下面是生产服务器的日志:

2016-09-21T14:53:45.788249+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/" host=www.ifurniture.pe request_id=4e9cd392-23bb-4160-8f2b-e9886cf25016 fwd="190.234.106.227" dyno= connect= service= status=503 bytes=

这是Subscribe_Controller。rb文件. .

class SubscribeController < ApplicationController
  def create
    logger.info "suscripcion #{params.inspect}"
    user = user_params(params)
    MailchimpWrapper.subscribe(user, params[:group_name], params[:group])
    redirect_to '/gracias'
    rescue Mailchimp::Error => e
      logger.error "ERROR mailchimp #{e.message} #{params.inspect}"
    end
  private def user_params(params)
    user = {
      email: params[:email].try(:downcase),
      first_name: params[:first_name].try(:titleize),
      last_name: params[:last_name].try(:titleize),
      city: params[:city].try(:titleize),
      phone: params[:phone],
      comment: params[:comment].try(:titleize)
    }
    user
  end
end

我在这样的新行中定义了这个方法…

  private 
  def user_params(params)
    user = {
      email: params[:email].try(:downcase),
      first_name: params[:first_name].try(:titleize),
      last_name: params[:last_name].try(:titleize),
      city: params[:city].try(:titleize),
      phone: params[:phone],
      comment: params[:comment].try(:titleize)
    }
    user
  end
end

并在生产服务器上得到这个错误…

2016-09-21T15:28:29.628087+00:00 app[web.1]: Started POST "/suscribir" for 190.234.106.227 at 2016-09-21 15:28:29 +0000
2016-09-21T15:28:29.635751+00:00 app[web.1]: Processing by SubscribeController#create as HTML
2016-09-21T15:28:29.635762+00:00 app[web.1]:   Parameters: {"utf8"=>"✓", "authenticity_token"=>"5sO+oVCDUD949rHpZz4RSxnSqpfXGZ9p9XwF+NO6HOkYPpfB95v1Wrn6EgxR7N+rEpVHOHuRtEaBAhNmoWTYVA==", "group_name"=>"suscriptores", "group"=>"newsletter", "email"=>"henry07_03@hotmail.com", "commit"=>"SUSCRIBIRSE"}
2016-09-21T15:28:29.635763+00:00 app[web.1]: suscripcion {"utf8"=>"✓", "authenticity_token"=>"5sO+oVCDUD949rHpZz4RSxnSqpfXGZ9p9XwF+NO6HOkYPpfB95v1Wrn6EgxR7N+rEpVHOHuRtEaBAhNmoWTYVA==", "group_name"=>"suscriptores", "group"=>"newsletter", "email"=>"henry07_03@hotmail.com", "commit"=>"SUSCRIBIRSE", "controller"=>"subscribe", "action"=>"create"}
2016-09-21T15:28:29.638002+00:00 app[web.1]: ERROR mailchimp You must provide a MailChimp API key {"utf8"=>"✓", "authenticity_token"=>"5sO+oVCDUD949rHpZz4RSxnSqpfXGZ9p9XwF+NO6HOkYPpfB95v1Wrn6EgxR7N+rEpVHOHuRtEaBAhNmoWTYVA==", "group_name"=>"suscriptores", "group"=>"newsletter", "email"=>"henry07_03@hotmail.com", "commit"=>"SUSCRIBIRSE", "controller"=>"subscribe", "action"=>"create"}
2016-09-21T15:28:29.657213+00:00 app[web.1]:   [1m[35mRefinery::Page Load (0.8ms)[0m  SELECT  "refinery_pages".* FROM "refinery_pages" WHERE "refinery_pages"."menu_match" = $1  ORDER BY "refinery_pages"."id" ASC LIMIT 1  [["menu_match", "^/404$"]]
2016-09-21T15:28:29.661415+00:00 app[web.1]:   [1m[36mRefinery::PagePart Load (2.1ms)[0m  [1mSELECT "refinery_page_parts".* FROM "refinery_page_parts" WHERE "refinery_page_parts"."refinery_page_id" IN (2)  ORDER BY position ASC[0m

我会注意你的回答。

我相信您需要在调用private后的新行上使用您的方法:

private 
  def user_params(params)
    user = {
      email: params[:email].try(:downcase),
      first_name: params[:first_name].try(:titleize),
      last_name: params[:last_name].try(:titleize),
      city: params[:city].try(:titleize),
      phone: params[:phone],
      comment: params[:comment].try(:titleize)
    }
    user
  end

从ruby版本2.1(!!)开始,方法定义返回一个符号作为方法名。

因此,下面的代码:
def my_method
  # ...
end
private :my_method

可以简化,就像你做的那样:

private def my_method
  # ...
end

查看堆栈跟踪,似乎您的生产服务器仍在运行ruby版本2.0.0。在这个较旧的ruby版本中,方法定义返回nil -因此您的代码无效。

理想情况下,您应该只更新生产ruby版本。如果没有这样的选项,可以这样写代码:

# Put this at the BOTTOM of the class
private 
def user_params(params)
  # ...
end

或:

# Put this anywhere in the class
def user_params(params)
  # ...
end
private :user_params

最新更新