instance method from_xml

Ruby on Rails 4.0.13

Since v3.0.20 Last seen in v4.2.9

Available in: v3.0.20 v3.1.12 v3.2.22.5 v4.0.13 v4.1.16 v4.2.9

Signature

from_xml(xml)

Sets the model attributes from a JSON string. Returns self.

class Person
  include ActiveModel::Serializers::Xml

  attr_accessor :name, :age, :awesome

  def attributes=(hash)
    hash.each do |key, value|
      instance_variable_set("@#{key}", value)
    end
  end

  def attributes
    instance_values
  end
end

xml = { name: 'bob', age: 22, awesome:true }.to_xml
person = Person.new
person.from_xml(xml) # => #<Person:0x007fec5e3b3c40 @age=22, @awesome=true, @name="bob">
person.name          # => "bob"
person.age           # => 22
person.awesome       # => true

Parameters

xml req
Source
# File activemodel/lib/active_model/serializers/xml.rb, line 232
      def from_xml(xml)
        self.attributes = Hash.from_xml(xml).values.first
        self
      end

Defined in activemodel/lib/active_model/serializers/xml.rb line 232 · View on GitHub · Improve this page · Find usages on GitHub

Defined in ActiveModel::Serializers::Xml

Type at least 2 characters to search.

↑↓ navigate · open · esc close