Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 941911
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T22:11:21+00:00 2026-05-15T22:11:21+00:00

I’m curious about the best practice in creating web services which support both XML

  • 0

I’m curious about the best practice in creating web services which support both XML and JSON output.

At the moment, my service is very XML oriented (see below), but I want to add JSON support.

Here’s one example:

  def create
    render_authentication_error and return if !@user
    render_parameter_not_found_error("item") and return if params[:item].nil?
    render_parameter_not_found_error("name") and return if params[:item][:name].blank?
    @item = Item.new(params[:item])
    begin 
      if !@item.save 
        raise "Item not saved" 
      end 
    rescue 
      render_resource_not_saved_error("Error saving #{@item.name}")
    else 
      render_resource_created(:location => item_path(@item))
    end 
  end

There are helper methods in ApplicationController that renders XML accordingly.

I’m not sure how to go about adding respond_to block here since I need to do validations. Is there a way to retrieve format in a controller outside of respond_to block so that I can output error messages in a given format as well?

respond_to do |format|
  format.html { render :json => @item.to_json }
  format.xml { render :xml => @item }
end 

Let me know if you need any clarification.

UPDATE I suppose I’m looking something similar to respond_with in Rails 3. I’m on Rails 2.3 still.. I wonder if it can be done in 2.3?

UPDATE Inherited Resources seems like what I’m looking for.

Thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T22:11:22+00:00Added an answer on May 15, 2026 at 10:11 pm

    Replace @person with @item, but there’s the idea:

    respond_to do |format|
      if @person.save 
        format.html { 
          flash[:notice] = 'Person was successfully created.'
          redirect_to @person 
        }
        format.js { render :json => @person, :status => :created, :location => @person} 
        format.xml  { render :xml => @person, :status => :created, :location => @person }
      else
        format.html { render :partial => "form" }
        format.js { render :json=>@person.errors, :status => :unprocessable_entity }
        format.xml  { render :xml => @person.errors, :status => :unprocessable_entity }
      end
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.