Ruby-on-rails Rails:返回 401?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2075645/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Rails: Return a 401?
提问by neezer
I'd like to return a HTTP 401 error as part of my permission_deniedmethod for declarative_authorization.
我想返回 HTTP 401 错误作为我permission_denied的declarative_authorization方法的一部分。
# triggered when a user accesses a page that they don't have access to
def permission_denied
# render my default 401 error page?
end
How would I do this? (Pardon the question if it's stupid... I know how to render the 401.html page in my public directory, but I don't think it returns the 401 HTTP header, which is what I'm after.)
我该怎么做?(请原谅这个问题,如果它很愚蠢......我知道如何在我的公共目录中呈现 401.html 页面,但我认为它不会返回 401 HTTP 标头,这正是我所追求的。)
回答by Garrett
You can add the :statusoption
您可以添加:status选项
def permission_denied
render :file => "public/401.html", :status => :unauthorized
end

