.to_xml not working properly when using Active Model Serializer

164 views Asked by At

I'm trying to render an xml response on my rails API using mime response. the code is like this :

respond_to do |format| format.xml { render xml: @records.to_xml, status: :ok } format.json { render json: @records, status: :ok } end

I'm using serializer on @records to properly shows the record needed. however it is only working on json format and not to XML format.

btw my serializer uses relation and im also using pagy to paginate the record.

I have both activemodel-serializer and activemodel-serializer-xml in my gemfile

sample: @records = Model.where(conditions)

result in XML format:

<?xml version="1.0" encoding="UTF-8"?>
 <models type="array">
    <model>
        <id type="integer">1</id>
        <name type="string"> name1 </name>
        <status type="string"> active </status>
    </model>
</models>

result in JSON format: { id: 1, name: 'name1', model_associate: [ { id: 1, name: 'ma_name1' } ] }

Expected output :

<?xml version="1.0" encoding="UTF-8"?>
 <models type="array">
    <model>
        <id type="integer">1</id>
        <name type="string"> name1 </name>
        <model_associates type="array">
           <model_associate>
              <id type="integer">1</id>
              <name type="string">ma_name1</name>
           <model_associate>
        </model_associates>
    </model>
</models>

Im using rails 7, ruby 3.0

1

There are 1 answers

5
Rajesh On

Answer 1:

01 to_xml can be used after installing activemodel-serializers-xml in your rails environment.

02 activemodel-serializers-xml can be installed in two ways

a) gem install activemodel-serializers-xml (But did not work in rails environment)

b) To work in rails environment add in your Gemfile or use bundle

  bundle add activemodel-serializers-xml
  bundle install

03 Create your Model and Include ActiveModel::Serializers::Xml

class User < ApplicationRecord
  include ActiveModel::Serializers::Xml
end 

04 Create you Controller and go to your favourite method eg. say 'show', find user and use to_xml as below.

class UsersController < ApplicationController
  def show
    @user = User.find(1)
    xml = @user.to_xml
    puts xml # You may check by printing on console.
    render xml: xml # render plain: xml
  end
end

Answer 2: As long as activemodel-serializers-xml is installed and recognised by your environment, it will work. To test it directly on console, use following program. save it as 'xmltest.rb' and run on console as:

  ruby xmltest.rb

If it does not work then activemodel-serializers-xml is not installed properly.

Code follows: Modify it for your database and Table

   require "active_record"
   require "activemodel-serializers-xml"

   ActiveRecord::Base.establish_connection(
     :adapter => "mysql2",
     :host => "127.0.0.1",
     :username => "root",
     :password => "",
     :database => "test",
   )

   class User < ActiveRecord::Base #ApplicationRecord
     include ActiveModel::Serializers::Xml
   end

   user = User.find(1)
   xml = user.to_xml

   puts xml