views:

206

answers:

1

Hey,

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

class ProductsController < ApplicationController
  before_filter :require_user
  ...
end
+3  A: 

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
tadman