xpepermint
xpepermint

Reputation: 36243

Rail status codes and XML

Controller:

class CategoriesController < ApplicationController
  def create
    @category = Category.create(...)
      respond_to do |format|
        if @category.save
          format.xml { :status => :created }
        else
          format.xml { :status => :unprocessable_entity }
        end
      end
    end
end

View:

xml.instruct! :xml, :version => "1.0" 
xml.response do
  xml.status( STATUS )
  xml.code( STATUS CODE )
end

As you can see I set a status code inside my create controller action. My question is how can I read this status code inside view (e.g. STATUS CODE should be a number like 200 for OK, STATUS should be string like "OK", "Unauthorized"). I know I could create a variable e.g. @status = 'ok' but I do not want to duplicate code. Thx for the answer!

Upvotes: 1

Views: 633

Answers (1)

John Topley
John Topley

Reputation: 115322

The way that you pass variables from a controller to a view in Rails is by using instance variables:

xml.instruct! :xml, :version => "1.0"  
xml.response do 
  xml.status(@status) 
  xml.code(@status_code)
end

However, I don't understand why the client would get the status and status code from the returned XML when that information is already available to it from the HTTP response i.e. HTTP 200 OK. Providing it in the XML as well is redundant.

Upvotes: 1

Related Questions