Ruby on rails 3 Rails 3密码保护单视图

Ruby on rails 3 Rails 3密码保护单视图,ruby-on-rails-3,password-protection,Ruby On Rails 3,Password Protection,我已经看过了一系列的屏幕放映(http://railscasts.com/episodes/270-authentication-in-rails-3-1)和教程(http://ruby.railstutorial.org/chapters/modeling-and-viewing-users-two?version=2.3#sec:secure_passwords)但我似乎无法找到或应用我正在学习的内容来创建一个受密码保护的单视图 到目前为止,我正在生成一个随机密码,并将其显示给用户,以便返回

我已经看过了一系列的屏幕放映(http://railscasts.com/episodes/270-authentication-in-rails-3-1)和教程(http://ruby.railstutorial.org/chapters/modeling-and-viewing-users-two?version=2.3#sec:secure_passwords)但我似乎无法找到或应用我正在学习的内容来创建一个受密码保护的单视图

到目前为止,我正在生成一个随机密码,并将其显示给用户,以便返回并查看他们上载的文件(使用SecureRandom.hex生成)。但是,我知道,当我将http_basic仅限于控制器中的show视图和方法时,使用它似乎不起作用:
http_basic\u authenticate\u with:password=>:passcode,:only=>:show

我知道这行代码不起作用,因为:passcode不引用创建的单个文件密码


此外,我知道http_basic_authenticate_with不是安全密码应该采用的方式,因此您将如何使用has_secure_password等进行此操作?

您可以尝试将http_basic_authenticate_with:password=>封装在控制器中的方法中,并调用
before\u filter
调用该方法。
诸如此类:

before_filter :restrict, :only => :show

def show
end

def restrict
  http_basic_authenticate_with :password => :passcode
end

使用该解决方案时,我遇到的唯一问题是,我遇到了错误“undefined method`http_basic_authenticate_with.”。http_basic_authenticate_with是否必须位于控制器的顶部?我不知道这是否有效,但请尝试使用self定义该方法。比如:
def self.restrict
。我最终实现了这一点:使用_http_basic do | user、password | password==@file.passcode&&user==“end”验证或请求