2012-07-11 72 views
0

我已经通过了,看了一堆屏幕演员(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),但我似乎无法找到或应用我正在学习创建单个视图密码保护。Rails 3密码保护单一视图

截至目前,我正在生成一个随机密码并显示给用户返回并查看他们上传的文件(使用SecureRandom.hex生成)。不过,我知道使用http_basic似乎并不工作,当我只限于显示视图和方法在我的控制器:http_basic_authenticate_with :password => :passcode, :only => :show

我知道该行代码不起作用,因为:密码不引用个人文件创建的密码。

此外,我知道http_basic_authenticate_with不是应该为安全密码完成的方式,那么您将如何使用has_secure_password等来解决此问题?

回答

0

您可以尝试将http_basic_authenticate_with :password => :passcode封装在控制器中的方法中,并调用before_filter来调用该方法。
类似的东西:

before_filter :restrict, :only => :show 

def show 
end 

def restrict 
    http_basic_authenticate_with :password => :passcode 
end 
+0

,我使用该解决方案时,你得到的唯一问题是,我得到的错误“未定义的方法'http_basic_authenticate_with”。” http_basic_authenticate_with是否必须位于控制器的顶部? – user1470511 2012-07-11 14:13:48

+0

我不知道这个工作,但是尝试用自己来定义方法。像:'def self.restrict'。 – squiter 2012-07-11 14:15:50

+1

我结束了这个工作: authenticate_or_request_with_http_basic do | user,password | 密码== @ file.passcode && user ==“” end – user1470511 2012-07-11 14:17:27