开发者

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 creat开发者_JAVA技巧e a variable e.g. @status = 'ok' but I do not want to duplicate code. Thx for the answer!


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜