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
精彩评论