Ruby on Rails HTTP 基本身份验证

发布于 2024-09-16 01:09:09 字数 119 浏览 1 评论 0原文

我刚刚开始学习 Ruby on Rails,有一个新手问题,到目前为止我还找不到答案。我有一个使用 HTTP 基本身份验证的小型应用程序,并且我认为有一些设计元素,我只想在请求经过身份验证时显示这些元素。我如何访问这些信息?

I am just starting to learn Ruby on Rails and have a newbie question that I have so far been unable to find the answer for. I have a small app that uses HTTP basic authentication and I have design elements in my view that I only want to display if the request is authenticated. How can I access this information?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

吖咩 2024-09-23 01:09:09

您可以使用在会话期间存储数据的会话哈希( http ://www.tutorialspoint.com/ruby-on-rails/rails-session-cookies.htm)我建议使用以下教程和 Http Basic Auth 来生成表单:railscasts.com/episodes/21 -super-simple-authentication

这是一些您可以使用

application_controller 的代码

  helper_method :admin?
  def authenticate
    authenticate_or_request_with_http_basic do |user_name, password|
      session[:user] = "admin" if user_name == 'admin' && password == 'password' 
    end
  end

private

  def admin?
    session[:user] == "admin"
  end

You can use the session hash which store data during your session( http://www.tutorialspoint.com/ruby-on-rails/rails-session-cookies.htm) I will recomend using the following tutorial with Http Basic Auth for generating the form: railscasts.com/episodes/21-super-simple-authentication

heres some code you could use

application_controller

  helper_method :admin?
  def authenticate
    authenticate_or_request_with_http_basic do |user_name, password|
      session[:user] = "admin" if user_name == 'admin' && password == 'password' 
    end
  end

private

  def admin?
    session[:user] == "admin"
  end
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文