活动模型XML序列化:未定义的方法`[]'为零:NilClass

时间:2014-11-10 16:29:16

标签: ruby-on-rails ruby xml activemodel

最初来自here,我发布了一个新主题,专注于解决围绕" to_xml"方法

我想做的事情:从ruby对象(Whois :: Record)转换为XML。

我目前正在使用ActiveModel但不是ActiveRecord(例如以前的thread

这是相关的控制器:

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

请注意 @whois Whois :: Record对象,但是包含此类对象的类(在 body property

观点:

<% if @whois != nil %>

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

<% end %>

请注意, to_json 方法工作正常,因此ruby抛出 nilClass异常这一事实非常奇怪。

以下是从到_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&#39;   /home/nico/.rvmruby(2.1.3)gems / activesupport-4.1.6 / lib / active_support / xml_mini.rb:119:在to_tag' /home/nico/.rvmruby (2.1.3) gems/activesupport-4.1.6/lib/active_support/core_ext/hash/conversions.rb:88:in块(2级)in_xml&#39;   /home/nico/.rvmruby(2.1.3)gems / activesupport-4.1.6 / lib / active_support / core_ext / hash / conversions.rb:88:在each' /home/nico/.rvmruby (2.1.3) gems/activesupport-4.1.6/lib/active_support/core_ext/hash/conversions.rb:88:in块中的to_xml&#39;   /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&#39;   /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:in to_xml&#39;   app / models / request.rb:26:在`to_xml&#39;

Byebug给了我这样的领导:

to_xml &gt;的ActiveSupport :: SafeBuffer :: OutpuBuffer#的 safe_concat

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

注意 html_safe?设置为&#39; true&#39;在这里,价值等于&#34; \ t&#34;

original_concat

文件activesupport / lib / active_support / core_ext / string / output_safety.rb,第132行  对original_concat的别名

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

当我走过去回到to_xml方法时,发脾气的是没有课程来调用方法。我将非常感谢任何帮助,谢谢。

修改

以下是我的模型中的类请求:

#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

我已经使用了这个,因为我已经理解了XML尝试为每个属性定义一个类型,并且在命名对象方面存在问题。这是thread about to_xml and objects in rails

0 个答案:

没有答案