TypeError(不能将 nil 转换为 Integer): [英] TypeError (can't convert nil into Integer):

查看:38
本文介绍了TypeError(不能将 nil 转换为 Integer):的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 rails 3 中开发了一个插件,它为模型提供了一个文档存储库.在任何模型中,您都可以附加任意数量的具有特定名称的文档.喜欢.

class Person 

效果很好.我知道它看起来很愚蠢,但今天我开始我的项目并发现它已经死了.无法弄清楚为什么会发生.尝试了很多来弄清楚但没有运气.新建和更新表单显示良好,但当我们创建或更新时,它确实会产生错误.

这是一个重大错误:

TypeError(不能将 nil 转换为 Integer):

我试图调试它,但它在进入控制器的创建或更新方法之前产生错误.

完整的错误日志是:

 故障安全响应期间的错误:ActionView::Template::Errornazar.hussain/.rvm/gems/ruby-1.9.2-p0/gems/activesupport-3.0.3/lib/active_support/whiny_nil.rb:48:in `method_missing'nazar.hussain/.rvm/gems/ruby-1.9.2-p0/gems/actionpack-3.0.3/lib/action_dispatch/http/parameters.rb:10:in`参数'nazar.hussain/.rvm/宝石/红宝石1.9.2-P0/宝石/ActionPack的-3.0.3/LIB/action_dispatch/中间件/模板/救援/diagnostics.erb:3:`__home_likewise_open____________nazar_hussain__rvm_gems_ruby_______p__gems_actionpack_______lib_action_dispatch_middleware_templates_rescues_diagnostics_erb__205668074_102092250__741834326'nazar.hussain/.rvm/gems/ruby-1.9.2-p0/gems/actionpack-3.0.3/lib/action_view/template.rb:135:in `block in render'nazar.hussain/.rvm/gems/ruby-1.9.2-p0/gems/activesupport-3.0.3/lib/active_support/notifications.rb:54:in `instrument'nazar.hussain/.rvm/gems/ruby-1.9.2-p0/gems/actionpack-3.0.3/lib/action_view/template.rb:127:in `render'nazar.hussain/.rvm/gems/ruby-1.9.2-p0/gems/actionpack-3.0.3/lib/action_view/render/rendering.rb:59:in `block in _render_template'nazar.hussain/.rvm/gems/ruby-1.9.2-p0/gems/activesupport-3.0.3/lib/active_support/notifications.rb:52:in `block in instrument'nazar.hussain/.rvm/gems/ruby-1.9.2-p0/gems/activesupport-3.0.3/lib/active_support/notifications/instrumenter.rb:21:in `instrument'nazar.hussain/.rvm/gems/ruby-1.9.2-p0/gems/activesupport-3.0.3/lib/active_support/notifications.rb:52:in `instrument'nazar.hussain/.rvm/gems/ruby-1.9.2-p0/gems/actionpack-3.0.3/lib/action_view/render/rendering.rb:56:in`_render_template'nazar.hussain/.rvm/gems/ruby-1.9.2-p0/gems/actionpack-3.0.3/lib/action_view/render/rendering.rb:26:in `render'nazar.hussain/osd/development/atlantis/vendor/plugins/active_scaffold/lib/extensions/action_view_rendering.rb:86:in `render_with_active_scaffold'nazar.hussain/.rvm/gems/ruby-1.9.2-p0/gems/actionpack-3.0.3/lib/action_dispatch/middleware/show_exceptions.rb:88:in `rescue_action_locally'nazar.hussain/.rvm/gems/ruby-1.9.2-p0/gems/actionpack-3.0.3/lib/action_dispatch/middleware/show_exceptions.rb:68:in`render_exception'nazar.hussain/.rvm/gems/ruby-1.9.2-p0/gems/actionpack-3.0.3/lib/action_dispatch/middleware/show_exceptions.rb:59:in `rescue in call'nazar.hussain/.rvm/gems/ruby-1.9.2-p0/gems/actionpack-3.0.3/lib/action_dispatch/middleware/show_exceptions.rb:46:in `call'nazar.hussain/.rvm/gems/ruby-1.9.2-p0/gems/railties-3.0.3/lib/rails/rack/logger.rb:13:in `call'nazar.hussain/.rvm/gems/ruby-1.9.2-p0/gems/rack-1.2.1/lib/rack/runtime.rb:17:in `call'nazar.hussain/.rvm/gems/ruby-1.9.2-p0/gems/activesupport-3.0.3/lib/active_support/cache/strategy/local_cache.rb:72:in `call'nazar.hussain/.rvm/gems/ruby-1.9.2-p0/gems/rack-1.2.1/lib/rack/lock.rb:11:in `block in call'<internal:prelude>:10:in `synchronize'nazar.hussain/.rvm/gems/ruby-1.9.2-p0/gems/rack-1.2.1/lib/rack/lock.rb:11:in `call'nazar.hussain/.rvm/gems/ruby-1.9.2-p0/gems/actionpack-3.0.3/lib/action_dispatch/middleware/static.rb:30:in `call'nazar.hussain/.rvm/gems/ruby-1.9.2-p0/gems/railties-3.0.3/lib/rails/application.rb:168:in `call'nazar.hussain/.rvm/gems/ruby-1.9.2-p0/gems/railties-3.0.3/lib/rails/application.rb:77:in `method_missing'nazar.hussain/.rvm/gems/ruby-1.9.2-p0/gems/railties-3.0.3/lib/rails/rack/log_tailer.rb:14:in `call'nazar.hussain/.rvm/gems/ruby-1.9.2-p0/gems/rack-1.2.1/lib/rack/content_length.rb:13:in `call'nazar.hussain/.rvm/gems/ruby-1.9.2-p0/gems/rack-1.2.1/lib/rack/handler/webrick.rb:52:in `service'nazar.hussain/.rvm/rubies/ruby-1.9.2-p0/lib/ruby/1.9.1/webrick/httpserver.rb:111:in `service'nazar.hussain/.rvm/rubies/ruby-1.9.2-p0/lib/ruby/1.9.1/webrick/httpserver.rb:70:in `run'nazar.hussain/.rvm/rubies/ruby-1.9.2-p0/lib/ruby/1.9.1/webrick/server.rb:183:in `block in start_thread'

希望有人关注这个问题.我会很感激的.

根据要求,我发布了代码:

控制器是一个简单的脚手架:有一条额外的线.@testing.build_documents 在创建和编辑

这是模型

类测试

这是插件代码

module AttachDocumentsAs模块连接器def attach_documents_as(*attachment_as)@as = 零@type_model = 'AttachDocumentsAs::Models::AttachedDocumentsDefaultType'attachment_as =attachment_as.to_a.flatten.compact.map(&:to_sym)@as =attachment_as.first如果attachment_as.size >1@type_model =attachment_as.second结尾class_inheritable_reader(@as)class_inheritable_reader(:atd_as)write_inheritable_attribute(:atd_as, @as)class_inheritable_reader(:atd_as_type)write_inheritable_attribute(:atd_as_type, @type_model)if @type_model.to_s.camelize.constantize.is_a?(Class)class_eval 做has_many @as, :as =>:attachable, :class_name=>"AttachDocumentsAs::Models::AttachedDocument"accepts_nested_attributes_for @as包括 AttachDocumentsAs::Attacher::Core结尾结尾救援名称错误将名称为 #{"#{@as}DocType".camelize.constantize} 的模型不退出"结尾结尾结尾模块 AttachDocumentsAs模块连接器模块核心def self.included(base)base.send :include, AttachDocumentsAs::Attacher::Core::InstanceMethodsbase.extend AttachDocumentsAs::Attacher::Core::ClassMethods结尾模块实例方法def build_documents(count=1)1.最多算self.send(self.send(:atd_as)).build结尾文档 = self.send(self.send(:atd_as))0.upto (count-1) 做 |i|docs[i].typeable = self.send(:atd_as_type).to_s.camelize.constantize.new结尾结尾结尾模块类方法结尾结尾结尾结尾

最后是表格

<%= simple_form_for(@testing, :html => {:multipart => true}) do |f|%><%= f.error_notification %><div class="inputs"><%= f.input :title %>

<div class="inputs"><%= f.simple_fields_for :xdocs 做 |x|%><% 如果 x.object.new_record?%><div class="inputs"><%= x.input :attachment, :as=>:file %><%= x.input :title, :as=>:string %><%= x.input :description, :as=>:text %><%= x.input :type, :as=>:attachment_type %>

<%结束%><%结束%>

<div class="actions"><%= f.button :submit %>

<%结束%>

解决方案

这是由于attachment_fu Plugin.我删除了插件,一切正常.我再次安装插件,出现同样的问题.所以很明显attachment_fu正在制造一些冲突.

I developed a plugin in rails 3, which servers a a document repository for models. In any model you can attach any number of documents with specific names. Like.

class Person < ActiveRecord::Base
   attach_documents_as :job_documents
   attach_documents_as :personal_documents
end

It was working very good. I know it looks to be stupid, but today i start my project and found it dead. Can not figure out why does it happens. Tried a lot to figure out but no luck. New and Update forms shows well but when ever we create or update it does generate an error.

It is giving a major error:

TypeError (can't convert nil into Integer):

I tried to debug it , but it generates error before entering to the create or update method of the controller.

and complete error log is:

    Error during failsafe response: ActionView::Template::Error
nazar.hussain/.rvm/gems/ruby-1.9.2-p0/gems/activesupport-3.0.3/lib/active_support/whiny_nil.rb:48:in `method_missing'
nazar.hussain/.rvm/gems/ruby-1.9.2-p0/gems/actionpack-3.0.3/lib/action_dispatch/http/parameters.rb:10:in `parameters'
nazar.hussain/.rvm/gems/ruby-1.9.2-p0/gems/actionpack-3.0.3/lib/action_dispatch/middleware/templates/rescues/diagnostics.erb:3:in `__home_likewise_open____________nazar_hussain__rvm_gems_ruby_______p__gems_actionpack_______lib_action_dispatch_middleware_templates_rescues_diagnostics_erb__205668074_102092250__741834326'
nazar.hussain/.rvm/gems/ruby-1.9.2-p0/gems/actionpack-3.0.3/lib/action_view/template.rb:135:in `block in render'
nazar.hussain/.rvm/gems/ruby-1.9.2-p0/gems/activesupport-3.0.3/lib/active_support/notifications.rb:54:in `instrument'
nazar.hussain/.rvm/gems/ruby-1.9.2-p0/gems/actionpack-3.0.3/lib/action_view/template.rb:127:in `render'
nazar.hussain/.rvm/gems/ruby-1.9.2-p0/gems/actionpack-3.0.3/lib/action_view/render/rendering.rb:59:in `block in _render_template'
nazar.hussain/.rvm/gems/ruby-1.9.2-p0/gems/activesupport-3.0.3/lib/active_support/notifications.rb:52:in `block in instrument'
nazar.hussain/.rvm/gems/ruby-1.9.2-p0/gems/activesupport-3.0.3/lib/active_support/notifications/instrumenter.rb:21:in `instrument'
nazar.hussain/.rvm/gems/ruby-1.9.2-p0/gems/activesupport-3.0.3/lib/active_support/notifications.rb:52:in `instrument'
nazar.hussain/.rvm/gems/ruby-1.9.2-p0/gems/actionpack-3.0.3/lib/action_view/render/rendering.rb:56:in `_render_template'
nazar.hussain/.rvm/gems/ruby-1.9.2-p0/gems/actionpack-3.0.3/lib/action_view/render/rendering.rb:26:in `render'
nazar.hussain/osd/development/atlantis/vendor/plugins/active_scaffold/lib/extensions/action_view_rendering.rb:86:in `render_with_active_scaffold'
nazar.hussain/.rvm/gems/ruby-1.9.2-p0/gems/actionpack-3.0.3/lib/action_dispatch/middleware/show_exceptions.rb:88:in `rescue_action_locally'
nazar.hussain/.rvm/gems/ruby-1.9.2-p0/gems/actionpack-3.0.3/lib/action_dispatch/middleware/show_exceptions.rb:68:in `render_exception'
nazar.hussain/.rvm/gems/ruby-1.9.2-p0/gems/actionpack-3.0.3/lib/action_dispatch/middleware/show_exceptions.rb:59:in `rescue in call'
nazar.hussain/.rvm/gems/ruby-1.9.2-p0/gems/actionpack-3.0.3/lib/action_dispatch/middleware/show_exceptions.rb:46:in `call'
nazar.hussain/.rvm/gems/ruby-1.9.2-p0/gems/railties-3.0.3/lib/rails/rack/logger.rb:13:in `call'
nazar.hussain/.rvm/gems/ruby-1.9.2-p0/gems/rack-1.2.1/lib/rack/runtime.rb:17:in `call'
nazar.hussain/.rvm/gems/ruby-1.9.2-p0/gems/activesupport-3.0.3/lib/active_support/cache/strategy/local_cache.rb:72:in `call'
nazar.hussain/.rvm/gems/ruby-1.9.2-p0/gems/rack-1.2.1/lib/rack/lock.rb:11:in `block in call'
      <internal:prelude>:10:in `synchronize'
nazar.hussain/.rvm/gems/ruby-1.9.2-p0/gems/rack-1.2.1/lib/rack/lock.rb:11:in `call'
nazar.hussain/.rvm/gems/ruby-1.9.2-p0/gems/actionpack-3.0.3/lib/action_dispatch/middleware/static.rb:30:in `call'
nazar.hussain/.rvm/gems/ruby-1.9.2-p0/gems/railties-3.0.3/lib/rails/application.rb:168:in `call'
nazar.hussain/.rvm/gems/ruby-1.9.2-p0/gems/railties-3.0.3/lib/rails/application.rb:77:in `method_missing'
nazar.hussain/.rvm/gems/ruby-1.9.2-p0/gems/railties-3.0.3/lib/rails/rack/log_tailer.rb:14:in `call'
nazar.hussain/.rvm/gems/ruby-1.9.2-p0/gems/rack-1.2.1/lib/rack/content_length.rb:13:in `call'
nazar.hussain/.rvm/gems/ruby-1.9.2-p0/gems/rack-1.2.1/lib/rack/handler/webrick.rb:52:in `service'
nazar.hussain/.rvm/rubies/ruby-1.9.2-p0/lib/ruby/1.9.1/webrick/httpserver.rb:111:in `service'
nazar.hussain/.rvm/rubies/ruby-1.9.2-p0/lib/ruby/1.9.1/webrick/httpserver.rb:70:in `run'
nazar.hussain/.rvm/rubies/ruby-1.9.2-p0/lib/ruby/1.9.1/webrick/server.rb:183:in `block in start_thread'

Hope some one will look in the issue. I will be thank full.

as requested i am posting the code:

Controller is simple scaffold: with an additional line. @testing.build_documents in create and edit

here is the model

class Testing < ActiveRecord::Base

  attach_documents_as :xdocs, :XdocsDocType

end

Here is the plugin code

module AttachDocumentsAs
  module Attacher

    def attach_documents_as(*attachment_as)
      @as           = nil
      @type_model   = 'AttachDocumentsAs::Models::AttachedDocumentsDefaultType'

      attachment_as = attachment_as.to_a.flatten.compact.map(&:to_sym)
      @as           = attachment_as.first

      if attachment_as.size > 1
        @type_model = attachment_as.second
      end

      class_inheritable_reader(@as)

      class_inheritable_reader(:atd_as)
      write_inheritable_attribute(:atd_as, @as)

      class_inheritable_reader(:atd_as_type)
      write_inheritable_attribute(:atd_as_type, @type_model)

      if @type_model.to_s.camelize.constantize.is_a?(Class)
        class_eval do
          has_many @as, :as => :attachable, :class_name=>"AttachDocumentsAs::Models::AttachedDocument"
          accepts_nested_attributes_for @as

          include AttachDocumentsAs::Attacher::Core
        end
      end

    rescue NameError
      puts "Model with name #{"#{@as}DocType".camelize.constantize} does not exits"
    end

  end
end

module AttachDocumentsAs
  module Attacher
    module Core

      def self.included(base)
        base.send :include, AttachDocumentsAs::Attacher::Core::InstanceMethods
        base.extend AttachDocumentsAs::Attacher::Core::ClassMethods
      end

      module InstanceMethods
        def build_documents(count=1)
          1.upto count do
            self.send(self.send(:atd_as)).build
          end
          docs = self.send(self.send(:atd_as))
          0.upto (count-1) do |i|
            docs[i].typeable = self.send(:atd_as_type).to_s.camelize.constantize.new
          end
        end
      end

      module ClassMethods
      end
    end
  end
end

and finally the form

<%= simple_form_for(@testing, :html => {:multipart => true}) do |f| %>
    <%= f.error_notification %>

    <div class="inputs">
      <%= f.input :title %>
    </div>

    <div class="inputs">
      <%= f.simple_fields_for :xdocs do |x| %>
          <% if x.object.new_record? %>
              <div class="inputs">
                <%= x.input :attachment, :as=>:file %>
                <%= x.input :title, :as=>:string %>
                <%= x.input :description, :as=>:text %>
                <%= x.input :type, :as=>:attachment_type %>
              </div>
          <% end %>
      <% end %>

    </div>

    <div class="actions">
      <%= f.button :submit %>
    </div>
<% end %>

解决方案

It was due to attachment_fu Plugin. I removed the plugin and every thing works fine. I install plugin again, the same problem arises. So it got clear that attachment_fu was creating some conflicts.

这篇关于TypeError(不能将 nil 转换为 Integer):的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆