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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 22:09:20  来源:igfitidea点击:

Rails: Return a 401?

ruby-on-railshttp

提问by neezer

I'd like to return a HTTP 401 error as part of my permission_deniedmethod for declarative_authorization.

我想返回 HTTP 401 错误作为我permission_denieddeclarative_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