开发者

Rails: request.xml?

I would like to check whether the request is XML od HTML. When HTML the page is redirected to login form (if a user is not logged in) and when XML the user get not authorized status code.

Example:

class ApplicationController < ActionController::Base
  def require_user
    unless current_user
      IF XML
        RESPOND WITH CODE
      ELSE
        redirect_to :controller => :user_sessions, :action => :new, :format => params[:format]
      END
      return false
    end
  end
end

cl开发者_运维技巧ass ProductsController < ApplicationController
  before_filter :require_user
  ...
end


You should be able to use the format delegation method:

unless (current_user)
  respond_to do |format|
    format.xml do
      # respond with code
    end

    format.html do
      redirect_to :controller => :user_sessions, :action => :new, :format => params[:format]
    end
  end

  return false
end
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜