Q How would you render Json and Xml in ruby on rails?

Actually guys it very simple to to send a json or xml response in the ruby on rails. But in most of the cases people didn’t knew about what is a json response and what is an xml response.

In an application we use json and xml responses to write the API for our application now a new question arises in mind What is and API Application Programming Interface it is used to enhance the capability of our web application so that it could interact with other website or devices over the network in terms of data exchange. But a good method is to write an Web service for data exchange over the network. But that is another vast area , however lets concentrate on our aim first.

Firstly lets create an new rails app by:

$rails new exercise -d mysql
$cd exercise 
$rails generate model user name:string email_id:string age:integer
$rails generate controller user index send_json send_xml
$rake db:migrate

open: exercise/app/controllers/users_controller

def send_json
@user=user.find_by_id(1)
render json:@user
end

def send_xml
@user=user.find_by_id(1)
render xml:@user
end

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Create a free website or blog at WordPress.com.

Up ↑

%d bloggers like this: