关于ruby on rails:TypeError(无法将nil转换为Integer):

TypeError (can't convert nil into Integer):

我在Rails 3中开发了一个插件,该插件用于管理模型的文档存储库。在任何模型中,您都可以附加任意数量的具有特定名称的文档。喜欢。

1
2
3
4
class Person < ActiveRecord::Base
   attach_documents_as :job_documents
   attach_documents_as :personal_documents
end

效果很好。我知道它看起来很愚蠢,但是今天我开始了我的项目,发现它死了。无法弄清楚为什么会发生。尝试了很多,但没有运气。"新建"和"更新"表单显示得很好,但是无论何时我们创建或更新它,都会产生错误。

出现严重错误:

1
TypeError (can't convert nil into Integer):

我尝试调试它,但是在进入控制器的create或update方法之前,它会生成错误。

,完整的错误日志为:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
    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'

希望有人会看到这个问题。我会很感激。

根据要求,我正在发布代码:

控制器是简单的支架:带有额外的一行。
@testing.build_documents在创建和编辑中

这是型号

1
2
3
4
5
class Testing < ActiveRecord::Base

  attach_documents_as :xdocs, :XdocsDocType

end

这是插件代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
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

最后是表格

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
<%= simple_form_for(@testing, :html => {:multipart => true}) do |f| %>
    <%= f.error_notification %>

   
      <%= f.input :title %>
   

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

   

   
      <%= f.button :submit %>
   
<% end %>


这是由于attachment_fu插件引起的。我删除了插件,一切正常。我再次安装插件,出现同样的问题。因此很明显,attachment_fu正在引起一些冲突。