Active Model XML serialization : undefined method `[]' for nil:NilClass

拥有回忆 提交于 2019-12-24 14:34:52

问题


Originally from here, i'm posting a new thread to focus on my issue resolving around the "to_xml" method.

What i'm trying to do : make a conversion from a ruby Object (Whois::Record) to XML.

I am currently using ActiveModel but not ActiveRecord (eg previous thread for reasons)

Here is the relevant controller :

def lookup
   domain = params[:domain]
   @whois = Request.new
   @whois.search(domain)

   respond_to do |format|
     if @whois != nil
       format.html
       format.json {render :json => @whois.body}
       format.xml {render :xml => @whois}
     else
       format.html { render :new }
       format.json { render json: @request.errors, status: :unprocessable_entity }
     end
   end
end

Please note that @whois is NOT a Whois::Record object, but a class that contains such object (in the body property)

And the view :

<% if @whois != nil %>

    <%= @whois %>
    <!--JSON-->
    <%= @whois.body.to_json %>
    <!--XML-->
    <% byebug %>
    <%= @whois.to_xml %>

<% end %>

Note that the to_json method works fine, so the fact that ruby throws a nilClass exception is quite weird.

Here is the stacktrace starting from to_xml :

/home/nico/.rvmruby (2.1.3) gems/activesupport-4.1.6/lib/active_support/xml_mini.rb:148:in _dasherize' /home/nico/.rvmruby (2.1.3) gems/activesupport-4.1.6/lib/active_support/xml_mini.rb:140:inrename_key' /home/nico/.rvmruby (2.1.3) gems/activesupport-4.1.6/lib/active_support/xml_mini.rb:119:in to_tag' /home/nico/.rvmruby (2.1.3) gems/activesupport-4.1.6/lib/active_support/core_ext/hash/conversions.rb:88:inblock (2 levels) in to_xml' /home/nico/.rvmruby (2.1.3) gems/activesupport-4.1.6/lib/active_support/core_ext/hash/conversions.rb:88:in each' /home/nico/.rvmruby (2.1.3) gems/activesupport-4.1.6/lib/active_support/core_ext/hash/conversions.rb:88:inblock in to_xml' /home/nico/.rvmruby (2.1.3) gems/builder-3.2.2/lib/builder/xmlbase.rb:175:in call' /home/nico/.rvmruby (2.1.3) gems/builder-3.2.2/lib/builder/xmlbase.rb:175:in_nested_structures' /home/nico/.rvmruby (2.1.3) gems/builder-3.2.2/lib/builder/xmlbase.rb:68:in tag!' /home/nico/.rvmruby (2.1.3) gems/activesupport-4.1.6/lib/active_support/core_ext/hash/conversions.rb:87:into_xml' app/models/request.rb:26:in `to_xml'

Byebug gives me a lead as such :

to_xml > ActiveSupport::SafeBuffer::OutpuBuffer#safe_concat

   153:     def safe_concat(value)
=> 154:       raise SafeConcatError unless html_safe?
   155:       original_concat(value)
   156:     end

note : html_safe? is set at 'true' here, and value equals "\t"

original_concat

File activesupport/lib/active_support/core_ext/string/output_safety.rb, line 132 Alias to original_concat

def concat(value)
  if !html_safe? || value.html_safe?
    super(value)
  else
    super(ERB::Util.h(value))
  end
end

And when i step over i come back to the to_xml method, that's throwing a tantrum about not having a class to call upon methhods. I would be grateful for any help, thanks.

Edit

Here is the class Request from my model :

#This is a generic class for checking Key authenticity and sending requests to the engine
#Furthermore, this class will contain the WHOIS response in body

include ActiveModel::Model
include ActiveModel::Serializers::JSON
include ActiveModel::Serializers::Xml

attr_accessor :body

def attributes
    {body => nil}
end


def to_xml(options = {})
    to_xml_opts = {:skip_types => true} # no type information, not such a great idea!
    to_xml_opts.merge!(options.slice(:builder, :skip_instruct))
    # a builder instance is provided when to_xml is called on a collection of instructors,
    # in which case you would not want to have <?xml ...?> added to each item
    to_xml_opts[:root] ||= "instructor"
    self.attributes.to_xml(to_xml_opts)
end

def search(domain)
    # since rails compiles the code before executing a search once,
    # we have to catch niClass exceptions and such
    if domain != ( nil && true && "" )
        c = Whois::Client.new
        self.body = c.lookup(domain)
    end
end

I've used this as from what i've understood XML tries to define a type for each attribute, and has problems with named objects. Here's the thread about to_xml and objects in rails

来源:https://stackoverflow.com/questions/26848385/active-model-xml-serialization-undefined-method-for-nilnilclass

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!