Jump to content

The ultimate community for Ruby on Rails developers.


Photo

Build an xml Api without using Database

I need Help

  • Please log in to reply
2 replies to this topic

#1 machipeddy

machipeddy

    Passenger

  • Members
  • 2 posts
  • LocationIndia

Posted 12 February 2014 - 07:59 AM

Hi

can any one help me out how to build an xml api with out using database .

 

Thanks,



#2 Ohm

Ohm

    Guard

  • Members
  • 179 posts
  • LocationCopenhagen

Posted 12 February 2014 - 04:43 PM

What would you have in the API if not data from a database?

 

In your controller, e.g. users_controller.rb, you can have an index method. Here we'll just respond to .xml and let the view handle the xml generation.

def index
  @users = User.all

  responds_to do |format|
    format.html # If you want to still let the html view render
    format.xml
  end
end

Inside our view folder, we'll have an users folder and in this we can create an index.xml.erb file. This could look like so:

<?xml version="1.0" encoding="UTF-8"?>
<users>
  <% @users.each do |user| %>
    <user>
      <name><%= user.name %></name>
      ...
    </user>
  <% end %>
</users>

Blog: http://ohm.sh | Twitter: madsohm


#3 machipeddy

machipeddy

    Passenger

  • Members
  • 2 posts
  • LocationIndia

Posted 13 February 2014 - 07:18 AM

Hi ohm,

 

Thanks for the reply but there is a scenario where  the data will be passed through the html forum to the api  without the database acess.






0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users