更新了gem,获取NoMethodError:nil:NilClass的未定义方法“ []”

时间:2019-04-12 07:00:00

标签: ruby-on-rails

我将项目更新为Rails 4.0,在更新了其他一些过时和冲突的gem之后(例如postgres_ext),我遇到了另一个问题。我单击链接来编辑用户并收到此错误:

ActionView::Template::Error (undefined method `[]' for nil:NilClass):
    3:     = hidden_field_tag :tab, 'personal'
    4:     %fieldset
    5:       .row-fluid
    6:         .span2= f.select :title, options_for_select(employee_title_options, employee.title), {:include_blank => true}, {:class => "span12", :autocomplete => 'off'}
    7:         .span4= f.text_field :first_name, :class => 'span12'
    8:         .span4= f.text_field :last_name, :class => 'span12'
    9:
  lib/bootstrap_form_builder.rb:6:in `label'
  lib/bootstrap_form_builder.rb:19:in `select'
  app/views/employees/tabs/_personal_edit.html.haml:6:in `block in _app_views_employees_tabs__personal_edit_html_haml__887033957300686763_70211507783580'
  app/views/employees/tabs/_personal_edit.html.haml:1:in `_app_views_employees_tabs__personal_edit_html_haml__887033957300686763_70211507783580'
  app/views/employees/edit.html.haml:46:in `_app_views_employees_edit_html_haml__2805730884185404262_70211428948540'

这是第6行的方法:

def #{selector}(name, options = {})
        options[:autocomplete] ||= 'off'
        @template.content_tag(:div,
          label(name, options.delete(:label), :class => 'control-label') +
          @template.content_tag(:div,
            super(name, options_with_errors(name, options)),
          :class => 'controls'),
        :class => class_with_errors(name, 'control-group'))
      end

和第19行方法:

def select(name, choices, options = {}, html_options = {})
      html_options[:autocomplete] ||= 'off'
      @template.content_tag(:div,
        label(name, options.delete(:label)) +
        @template.content_tag(:div,
          super(name, choices, options, html_options),
        :class => 'controls'),
      :class => 'control-group')
    end
  end

使用“撬”时,我得到了:

[1] pry(#<BootstrapFormBuilder>)> options[:autocomplete] ||= 'off'
NoMethodError: undefined method `[]' for nil:NilClass
from (pry):1:in `label'

到目前为止,我对每个已更新的宝石都运行了“ bundle update“ gem””。我不确定应该更新哪些其他宝石。这是我的捆绑包:

 $ bundle show
Gems included by the bundle:
  * actionmailer (4.0.0)
  * actionpack (4.0.0)
  * activemodel (4.0.0)
  * activerecord (4.0.0)
  * activerecord-deprecated_finders (1.0.4)
  * activesupport (4.0.0)
  * addressable (2.3.6)
  * annotate (2.6.3)
  * arel (4.0.2)
  * authlogic (3.4.1)
  * autoparse (0.3.3)
  * awesome_print (1.2.0)
  * aws-eventstream (1.0.2)
  * aws-partitions (1.149.0)
  * aws-sdk (1.38.0)
  * aws-sdk-core (3.48.3)
  * aws-sdk-kms (1.16.0)
  * aws-sdk-s3 (1.36.0)
  * aws-sigv4 (1.1.0)
  * bcrypt (3.1.7)
  * bcrypt-ruby (3.1.5)
  * builder (3.1.4)
  * bundler (1.17.3)
  * capybara (2.2.1)
  * celluloid (0.15.2)
  * celluloid-io (0.15.0)
  * ckeditor (4.0.6 f0d175a)
  * climate_control (0.0.3)
  * cocaine (0.5.4)
  * coderay (1.1.0)
  * coffee-rails (4.2.2)
  * coffee-script (2.4.1)
  * coffee-script-source (1.12.2)
  * concurrent-ruby (1.1.5)
  * daemons (1.1.9)
  * dalli (2.7.0)
  * database_cleaner (1.2.0)
  * delayed_job (4.0.1)
  * delayed_job_active_record (4.0.1)
  * diff-lcs (1.2.5)
  * dotenv (0.7.0)
  * erubis (2.7.0)
  * eventmachine (1.0.3)
  * exception_notification (4.0.1)
  * execjs (2.7.0)
  * extlib (0.9.16)
  * factory_girl (4.4.0)
  * factory_girl_rails (4.4.1)
  * faraday (0.9.0)
  * ffi (1.9.0)
  * ffi-compiler (0.1.3)
  * font_assets (0.1.11)
  * foreigner (1.6.1)
  * foreman (0.64.0)
  * formatador (0.2.4)
  * google-api-client (0.7.1)
  * grocer (0.5.0)
  * guard (2.6.0)
  * guard-rspec (4.2.8)
  * haml (4.0.5)
  * haml-rails (0.4)
  * heroku-rglpk (0.2.1 760859c)
  * hike (1.2.3)
  * i18n (0.9.5)
  * jmespath (1.4.0)
  * jquery-rails (3.1.0)
  * jquery-ui-rails (4.2.1)
  * json (1.8.6)
  * jwt (0.1.11)
  * launchy (2.4.2)
  * listen (2.7.1)
  * lumberjack (1.0.5)
  * mail (2.5.5)
  * memcachier (0.0.2)
  * method_source (0.8.2)
  * mime-types (1.25.1)
  * mini_portile (0.5.3)
  * minitest (4.7.5)
  * multi_json (1.13.1)
  * multipart-post (2.0.0)
  * newrelic_rpm (3.7.3.204)
  * nio4r (1.2.1)
  * nokogiri (1.6.1)
  * oauth (0.5.3)
  * orm_adapter (0.5.0)
  * paperclip (3.5.4)
  * pg (0.17.1)
  * pg_array_parser (0.0.9)
  * polyglot (0.3.5)
  * postgres_ext (3.0.1)
  * protected_attributes (1.0.3)
  * pry (0.9.12.6)
  * quiet_assets (1.0.2)
  * rack (1.5.5)
  * rack-cors (0.2.9)
  * rack-test (0.6.3)
  * rails (4.0.0)
  * rails_12factor (0.0.2)
  * rails_serve_static_assets (0.0.2)
  * rails_stdout_logging (0.0.3)
  * railties (4.0.0)
  * rake (12.3.2)
  * rb-fsevent (0.9.4)
  * rb-inotify (0.9.3)
  * request_store (1.0.5)
  * retriable (1.4.1)
  * rspec (2.14.1)
  * rspec-core (2.14.8)
  * rspec-expectations (2.14.5)
  * rspec-mocks (2.14.6)
  * rspec-rails (2.14.2)
  * rubyzip (1.1.7)
  * sass (3.4.25)
  * sass-rails (5.0.7)
  * scrypt (1.2.1)
  * shortener (0.3.0)
  * signet (0.5.0)
  * slop (3.5.0)
  * spork (0.9.2)
  * sprockets (2.12.5)
  * sprockets-rails (2.0.1)
  * strip_attributes (1.5.1)
  * thin (1.6.2)
  * thor (0.20.3)
  * thread_safe (0.3.6)
  * tilt (1.4.1)
  * timers (1.1.0)
  * treetop (1.4.15)
  * trim_blobs (0.0.1)
  * twilio-ruby (3.11.5)
  * tzinfo (0.3.55)
  * uglifier (2.5.0)
  * uuidtools (2.1.4)
  * xeroizer (2.16.5)
  * xpath (2.0.0)
  * yui-compressor (0.12.0)

任何有关罪魁祸首的帮助将不胜感激。

到目前为止,这是我尝试使用的方法,我将binding.pry放在第19行上方,即'html_options [:autocomplete] || ='off':

[1] pry(#<BootstrapFormBuilder>)> @template.content_tag(:div, label(name, options.delete(:label)) + @template.content_tag(:div, super(name, choices, options, html_options), :class => 'controls'), :class => 'control-group')
NoMethodError: undefined method `[]' for nil:NilClass
from /Users/justinhung/projects/testDir/silk-hospitality/lib/bootstrap_form_builder.rb:5:in `label'
[2] pry(#<BootstrapFormBuilder>)>
[3] pry(#<BootstrapFormBuilder>)>
[4] pry(#<BootstrapFormBuilder>)> html_options[:autocomplete]
=> "off"
[5] pry(#<BootstrapFormBuilder>)> html_options[:autocomplete] ||= 'off'
=> "off"
[6] pry(#<BootstrapFormBuilder>)> @template.content_tag(:div, label(name, options.delete(:label)) + @template.content_tag(:div, super(name, choices, options, html_options), :class => 'controls'), :class => 'control-group')
NoMethodError: undefined method `[]' for nil:NilClass
from /Users/justinhung/projects/testDir/silk-hospitality/lib/bootstrap_form_builder.rb:5:in `label'
[7] pry(#<BootstrapFormBuilder>)> options[:autocomplete] ||= 'off'
=> "off"
[8] pry(#<BootstrapFormBuilder>)> label(name, options.delete(:label))
NoMethodError: undefined method `[]' for nil:NilClass
from /Users/justinhung/projects/testDir/silk-hospitality/lib/bootstrap_form_builder.rb:5:in `label'
[9] pry(#<BootstrapFormBuilder>)> super(name, choices, options, html_options)
=> "<select autocomplete=\"off\" class=\"span12\" id=\"employee_title\" name=\"employee[title]\"><option value=\"\"></option>\n<option value=\"Mr\">Mr</option>\n<option value=\"Mrs\">Mrs</option>\n<option selected=\"selected\" value=\"Ms\">Ms</option>\n<option value=\"Miss\">Miss</option></select>"
[10] pry(#<BootstrapFormBuilder>)> options
=> {:include_blank=>true, :autocomplete=>"off"}
[11] pry(#<BootstrapFormBuilder>)> label(name, options.delete(:label))
NoMethodError: undefined method `[]' for nil:NilClass
from /Users/justinhung/projects/testDir/silk-hospitality/lib/bootstrap_form_builder.rb:5:in `label'
[12] pry(#<BootstrapFormBuilder>)> label
ArgumentError: wrong number of arguments (0 for 1..2)
from /Users/justinhung/projects/testDir/silk-hospitality/lib/bootstrap_form_builder.rb:4:in `label'
[13] pry(#<BootstrapFormBuilder>)> label(1)
ArgumentError: wrong number of arguments (3 for 1..2)
from /Users/justinhung/projects/testDir/silk-hospitality/lib/bootstrap_form_builder.rb:4:in `label'
[14] pry(#<BootstrapFormBuilder>)> label(1,1,1)
ArgumentError: wrong number of arguments (3 for 1..2)
from /Users/justinhung/projects/testDir/silk-hospitality/lib/bootstrap_form_builder.rb:4:in `label'
[15] pry(#<BootstrapFormBuilder>)> label(1,1)
TypeError: no implicit conversion of Symbol into Integer
from /Users/justinhung/projects/testDir/silk-hospitality/lib/bootstrap_form_builder.rb:5:in `[]'
[16] pry(#<BootstrapFormBuilder>)> label(:class, :class)
TypeError: no implicit conversion of Symbol into Integer
from /Users/justinhung/projects/testDir/silk-hospitality/lib/bootstrap_form_builder.rb:5:in `[]'
[17] pry(#<BootstrapFormBuilder>)> label(name, class)
SyntaxError: unexpected ')'
[17] pry(#<BootstrapFormBuilder>)> label(name, name)
TypeError: no implicit conversion of Symbol into Integer
from /Users/justinhung/projects/testDir/silk-hospitality/lib/bootstrap_form_builder.rb:5:in `[]'
[18] pry(#<BootstrapFormBuilder>)>
ActionView::Template::Error (wrong number of arguments (3 for 1..2)):
    3:     = hidden_field_tag :tab, 'personal'
    4:     %fieldset
    5:       .row-fluid
    6:         .span2= f.select :title, options_for_select(employee_title_options, employee.title), {:include_blank => true}, {:class => "span12", :autocomplete => 'off'}
    7:         .span4= f.text_field :first_name, :class => 'span12'
    8:         .span4= f.text_field :last_name, :class => 'span12'
    9:
  lib/bootstrap_form_builder.rb:13:in `label'
  lib/bootstrap_form_builder.rb:18:in `label'
  lib/bootstrap_form_builder.rb:44:in `select'
  app/views/employees/tabs/_personal_edit.html.haml:6:in `block in _app_views_employees_tabs__personal_edit_html_haml___2427532747449540069_70152554531760'
  app/views/employees/tabs/_personal_edit.html.haml:1:in `_app_views_employees_tabs__personal_edit_html_haml___2427532747449540069_70152554531760'
  app/views/employees/edit.html.haml:46:in `_app_views_employees_edit_html_haml__3872729914300479182_70152556676880'

“标签”在这里:

def #{selector}(name, options = {})
        options[:autocomplete] ||= 'off'
        @template.content_tag(:div,
          label(name, options.fetch(:label, {}), :class => 'control-label') +
          @template.content_tag(:div,
            super(name, options_with_errors(name, options)),
            :class => 'controls'),
        :class => class_with_errors(name, 'control-group'))
      end

1 个答案:

答案 0 :(得分:1)

似乎label(name, options.delete(:label))的委派是nil。 您在haml中的选择是{:include_blank => true}。哈希中没有label键。 您应该使用options.fetch(:label, {})

将您的方法更改为:

def select(name, choices, options = {}, html_options = {})
  html_options[:autocomplete] ||= 'off'
  @template.content_tag(
    :div,
    label(
      name,
      options.fetch(:label, {})
    ) +
      @template.content_tag(
        :div,
        super(name, choices, options, html_options),
        :class => 'controls'
      ),
    :class => 'control-group'
  )
end

和:

def #{selector}(name, options = {})
  options[:autocomplete] ||= 'off'
  @template.content_tag(
    :div,
    label(name, options.fetch(:label, {}), :class => 'control-label') +
      @template.content_tag(
        :div,
        super(name, options_with_errors(name, options)),
        :class => 'controls'
      ),
    :class => class_with_errors(name, 'control-group')
  )
end

通知:options.fetch(:label, {})