2012-07-11 73 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