Active Model XML serialization: undefined method `[] 'for nil: NilClass
Originally from here , I am posting a new thread to focus on my problem resolving the "to_xml" method.
What I am trying to do: do a conversion from Ruby Object (Whois :: Record) to XML.
I am currently using ActiveModel but not ActiveRecord (like 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
Note that @whois is NOT a Whois :: Record object, but the class that contains such an 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 ββis throwing a nilClass exception is rather strange.
Here is the stacktrace starting from before_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:in
rename_key '/home/nico/.rvmruby(2.1.3) gems / activesupport-4.1 .6 / lib / active_support / xml_mini.rb: 119: intoto_tag' /home/nico/.rvmruby (2.1.3) gems/activesupport-4.1.6/lib/active_support/core_ext/hash/conversions.rb:88:in
block (2 levels) in to_xml '/home/nico/.rvmruby(2.1.3) gems / activesupport-4.1.6 / lib / active_support / core_ext / hash / convertions.rb: 88: ineach' /home/nico/.rvmruby (2.1.3) gems/activesupport-4.1.6/lib/active_support/core_ext/hash/conversions.rb:88:in
block in to_xml '/home/nico/.rvmruby(2.1.3) gems / builder-3.2.2 / lib / builder / xmlbase.rb: 175: incall' /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: intag!' /home/nico/.rvmruby (2.1.3) gems/activesupport-4.1.6/lib/active_support/core_ext/hash/conversions.rb:87:in
to_xml 'app / models / request.rb: 26: in `to_xml'
Byebug gives me an advantage 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? here is set to "true" and value is "\ t"
original_concat
File activesupport / lib / active_support / core_ext / string / output_safety.rb, line 132 Alias ββfor 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 return to the to_xml method, which throws a fit for not having a class to call the method. I would appreciate any help, thanks.
Edit
Here is a request for a class 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 used this as from what I understood that XML is trying to define a type for each attribute and has problems with named objects. Here's a stream of about_xml and objects in rails
source to share
No one has answered this question yet
See similar questions:
or similar: