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



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


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 whe 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.


No comments:

Post a Comment