`undefined方法`[]'为nil:Rails 4.0.1和HAML中的NilClass`

时间:2013-11-27 08:35:13

标签: ruby-on-rails activerecord haml

编辑#2:我刚刚从4.0.1降级回Rails 4.0.0并且它解决了问题...如果有人知道是什么导致它以及如何安全地升级到4.0 .1,请让我知道。或者这可能是应该报告的4.0.1或HAML的问题?我不知道在哪里举报......


我的HAML视图,昨天工作正常,我没有改变,不知何故不再工作了。

观点:

1 =form_for @document do |f|
2
3  =f.text_field :title, placeholder: "Title"
4
5  =f.text_field :slug, placeholder: "URL slug"
6
7  ... and more

它从第1行抛出的错误:

undefined method `[]' for nil:NilClass

@document在控制器中实例化为Document.new。就像我说的,这段代码昨天工作正常,我没有改变视图或控制器。我今天从Rails 4.0.0升级到4.0.1 - 这可能是罪魁祸首吗?

我的Gemfile.lock

GIT
  remote: git://github.com/adzap/active_enum.git
  revision: 280548e3954d8f9e032968dd2757b7e5862cdf49
  specs:
    active_enum (0.9.12)
      activesupport (>= 3.0)

GIT

  remote: git://github.com/sporkrb/spork-rails.git
  revision: 0dd45e59d3237b4c8f9efc215b46d9c07072a95e
  specs:
    spork-rails (4.0.0)
      rails (>= 3.0.0, < 5)
      spork (>= 1.0rc0)

GEM
  remote: https://rubygems.org/
  specs:
    actionmailer (4.0.1)
      actionpack (= 4.0.1)
      mail (~> 2.5.4)
    actionpack (4.0.1)
      activesupport (= 4.0.1)
      builder (~> 3.1.0)
      erubis (~> 2.7.0)
      rack (~> 1.5.2)
      rack-test (~> 0.6.2)
    active_model_serializers (0.8.1)
      activemodel (>= 3.0)
    activemodel (4.0.1)
      activesupport (= 4.0.1)
      builder (~> 3.1.0)
    activerecord (4.0.1)
      activemodel (= 4.0.1)
      activerecord-deprecated_finders (~> 1.0.2)
      activesupport (= 4.0.1)
      arel (~> 4.0.0)
    activerecord-deprecated_finders (1.0.3)
    activesupport (4.0.1)
      i18n (~> 0.6, >= 0.6.4)
      minitest (~> 4.2)
      multi_json (~> 1.3)
      thread_safe (~> 0.1)
      tzinfo (~> 0.3.37)
    annotate (2.5.0)
      rake
    ansi (1.4.3)
    arel (4.0.1)
    atomic (1.1.14)
    aws-sdk (1.22.1)
      json (~> 1.4)
      nokogiri (>= 1.4.4, < 1.6.0)
      uuidtools (~> 2.1)
    bcrypt-ruby (3.1.2)
    bootstrap-sass (2.3.1.0)
      sass (~> 3.2)
    builder (3.1.4)
    capybara (2.1.0)
      mime-types (>= 1.16)
      nokogiri (>= 1.3.3)
      rack (>= 1.0.0)
      rack-test (>= 0.5.4)
      xpath (~> 2.0)
    capybara-webkit (1.0.0)
      capybara (~> 2.0, >= 2.0.2)
      json
    celluloid (0.15.2)
      timers (~> 1.1.0)
    childprocess (0.3.9)
      ffi (~> 1.0, >= 1.0.11)
    climate_control (0.0.3)
      activesupport (>= 3.0)
    cocaine (0.5.2)
      climate_control (>= 0.0.3, < 1.0)
    coderay (1.0.9)
    coffee-rails (4.0.0)
      coffee-script (>= 2.2.0)
      railties (>= 4.0.0.beta, < 5.0)
    coffee-script (2.2.0)
      coffee-script-source
      execjs
    coffee-script-source (1.5.0)
    columnize (0.3.6)
    database_cleaner (1.1.1)
    debugger (1.6.2)
      columnize (>= 0.3.1)
      debugger-linecache (~> 1.2.0)
      debugger-ruby_core_source (~> 1.2.3)
    debugger-linecache (1.2.0)
    debugger-ruby_core_source (1.2.3)
    devise (3.1.1)
      bcrypt-ruby (~> 3.0)
      orm_adapter (~> 0.1)
      railties (>= 3.2.6, < 5)
      thread_safe (~> 0.1)
      warden (~> 1.2.3)
    diff-lcs (1.2.4)
    dropbox-sdk (1.6.1)
      json
    erubis (2.7.0)
    ethon (0.5.12)
      ffi (>= 1.3.0)
      mime-types (~> 1.18)
    execjs (1.4.0)
      multi_json (~> 1.0)
    factory_girl (4.1.0)
      activesupport (>= 3.0.0)
    factory_girl_rails (4.1.0)
      factory_girl (~> 4.1.0)
      railties (>= 3.0.0)
    faker (1.2.0)
      i18n (~> 0.5)
    fakeweb (1.3.0)
    faraday (0.8.8)
      multipart-post (~> 1.2.0)
    ffi (1.9.0)
    figaro (0.7.0)
      bundler (~> 1.0)
      rails (>= 3, < 5)
    font-awesome-sass-rails (3.0.2.2)
      railties (>= 3.1.1)
      sass-rails (>= 3.1.1)
    formatador (0.2.4)
    guard (2.0.5)
      formatador (>= 0.2.4)
      listen (~> 2.0)
      lumberjack (~> 1.0)
      pry (>= 0.9.12)
      thor (>= 0.18.1)
    guard-rspec (1.2.1)
      guard (>= 1.1)
    guard-spork (1.5.1)
      childprocess (>= 0.2.3)
      guard (>= 1.1)
      spork (>= 0.8.4)
    haml (4.0.4)
      tilt
    hashie (2.0.5)
    hashr (0.0.22)
    hike (1.2.3)
    httpauth (0.2.0)
    i18n (0.6.5)
    jquery-rails (3.0.1)
      railties (>= 3.0, < 5.0)
      thor (>= 0.14, < 2.0)
    json (1.8.0)
    jwt (0.1.8)
      multi_json (>= 1.5)
    libv8 (3.16.14.3)
    listen (2.1.0)
      celluloid (>= 0.15.2)
      rb-fsevent (>= 0.9.3)
      rb-inotify (>= 0.9)
    lumberjack (1.0.4)
    mail (2.5.4)
      mime-types (~> 1.16)
      treetop (~> 1.4.8)
    method_source (0.8.2)
    mime-types (1.25.1)
    minitest (4.7.5)
    multi_json (1.8.2)
    multipart-post (1.2.0)
    nokogiri (1.5.9)
    oauth2 (0.8.1)
      faraday (~> 0.8)
      httpauth (~> 0.1)
      jwt (~> 0.1.4)
      multi_json (~> 1.0)
      rack (~> 1.2)
    omniauth (1.1.4)
      hashie (>= 1.2, < 3)
      rack
    omniauth-github (1.1.0)
      omniauth (~> 1.0)
      omniauth-oauth2 (~> 1.1)
    omniauth-oauth2 (1.1.1)
      oauth2 (~> 0.8.0)
      omniauth (~> 1.0)
    orm_adapter (0.4.0)
    paperclip (3.5.1)
      activemodel (>= 3.0.0)
      activesupport (>= 3.0.0)
      cocaine (~> 0.5.0)
      mime-types
    pg (0.15.1)
    polyglot (0.3.3)
    protected_attributes (1.0.3)
      activemodel (>= 4.0.0, < 5.0)
    pry (0.9.12.2)
      coderay (~> 1.0.5)
      method_source (~> 0.8)
      slop (~> 3.4)
    rack (1.5.2)
    rack-test (0.6.2)
      rack (>= 1.0)
    rails (4.0.1)
      actionmailer (= 4.0.1)
      actionpack (= 4.0.1)
      activerecord (= 4.0.1)
      activesupport (= 4.0.1)
      bundler (>= 1.3.0, < 2.0)
      railties (= 4.0.1)
      sprockets-rails (~> 2.0.0)
    rails_12factor (0.0.2)
      rails_serve_static_assets
      rails_stdout_logging
    rails_serve_static_assets (0.0.1)
    rails_stdout_logging (0.0.3)
    railties (4.0.1)
      actionpack (= 4.0.1)
      activesupport (= 4.0.1)
      rake (>= 0.8.7)
      thor (>= 0.18.1, < 2.0)
    rake (10.1.0)
    rb-fsevent (0.9.3)
    rb-inotify (0.9.2)
      ffi (>= 0.5.0)
    ref (1.0.5)
    rest-client (1.6.7)
      mime-types (>= 1.16)
    rspec-core (2.14.7)
    rspec-expectations (2.14.3)
      diff-lcs (>= 1.1.3, < 2.0)
    rspec-mocks (2.14.3)
    rspec-rails (2.14.0)
      actionpack (>= 3.0)
      activesupport (>= 3.0)
      railties (>= 3.0)
      rspec-core (~> 2.14.0)
      rspec-expectations (~> 2.14.0)
      rspec-mocks (~> 2.14.0)
    sass (3.2.12)
    sass-rails (4.0.0)
      railties (>= 4.0.0.beta, < 5.0)
      sass (>= 3.1.10)
      sprockets-rails (~> 2.0.0)
    slop (3.4.6)
    spork (1.0.0rc4)
    sprockets (2.10.1)
      hike (~> 1.2)
      multi_json (~> 1.0)
      rack (~> 1.0)
      tilt (~> 1.1, != 1.3.0)
    sprockets-rails (2.0.1)
      actionpack (>= 3.0)
      activesupport (>= 3.0)
      sprockets (~> 2.8)
    therubyracer (0.12.0)
      libv8 (~> 3.16.14.0)
      ref
    thor (0.18.1)
    thread_safe (0.1.3)
      atomic
    tilt (1.4.1)
    timers (1.1.0)
    tire (0.6.1)
      activemodel (>= 3.0)
      activesupport
      ansi
      hashr (~> 0.0.19)
      multi_json (~> 1.3)
      rake
      rest-client (~> 1.6)
    treetop (1.4.15)
      polyglot
      polyglot (>= 0.3.1)
    typhoeus (0.6.3)
      ethon (~> 0.5.11)
    tzinfo (0.3.38)
    uglifier (2.2.1)
      execjs (>= 0.3.0)
      multi_json (~> 1.0, >= 1.0.2)
    uuidtools (2.1.4)
    vcr (2.5.0)
    warden (1.2.3)
      rack (>= 1.0)
    xpath (2.0.0)
      nokogiri (~> 1.3)

PLATFORMS
  ruby

DEPENDENCIES
  active_enum!
  active_model_serializers
  annotate (= 2.5.0)
  aws-sdk
  bootstrap-sass (= 2.3.1.0)
  capybara (= 2.1.0)
  capybara-webkit (= 1.0.0)
  childprocess (= 0.3.9)
  coffee-rails (= 4.0.0)
  coffee-script-source (= 1.5.0)
  database_cleaner (~> 1.1.1)
  debugger
  devise (= 3.1.1)
  dropbox-sdk (= 1.6.1)
  execjs (= 1.4.0)
  factory_girl_rails (= 4.1.0)
  faker (~> 1.2.0)
  fakeweb (= 1.3.0)
  figaro (= 0.7.0)
  font-awesome-sass-rails (= 3.0.2.2)
  guard-rspec (= 1.2.1)
  guard-spork (= 1.5.1)
  haml
  jquery-rails (= 3.0.1)
  json (= 1.8.0)
  nokogiri (= 1.5.9)
  omniauth (= 1.1.4)
  omniauth-github (= 1.1.0)
  paperclip
  pg (= 0.15.1)
  protected_attributes
  rails (= 4.0.1)
  rails_12factor
  rspec-mocks
  rspec-rails
  sass-rails
  spork-rails!
  therubyracer (= 0.12.0)
  tire
  typhoeus (= 0.6.3)
  uglifier (>= 1.0.3)
  vcr (= 2.5.0)

另外,我刚刚注意到当我加载Rails控制台时,当我在任何 ActiveRecord模型上调用.new时,我现在收到以下错误:{{1} }。这两个错误可能有关系吗?发生了什么事?

编辑:根据要求,这是完整的错误跟踪:

Object doesn't support #inspect

2 个答案:

答案 0 :(得分:10)

看起来它与protected_attributes gem的版本相关,与rails 4.0.1不利。尝试升级到protected_attributes 1.0.5。

https://github.com/thoughtbot/factory_girl_rails/issues/115

答案 1 :(得分:0)

pra mim resolveu assim。

params[:adm_video][:codigo] =https://www.youtube.com/watch?v=73egYHwYHzQ    

@adm_video[:codigo] = parse_youtube(params[:adm_video][:codigo])

    def parse_youtube(url)
     if !url.blank?
       regex = /(?:.be\/|\/watch\?v=|\/(?=p\/))([\w\/\-]+)/
       return  url.match(regex)[1]
     end
   end

return =&#34; 73egYHwYHzQ&#34;插入数据库。