views:

151

answers:

1

Hey...

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!

+1  A: 

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.

John Topley
Hum... redundant yes i guess you are right. I just thought it would be nice to alwais have some kind of response with some additional error message.
xpepermint