2016-09-23 5 views
0

認証が失敗した場合、私はroot_pathにリダイレクトしようとしています。 現在、HTTP Basicを使用してページの本文を変更しています。アクセスが拒否されました。ここでRails authenticate_or_request_with_http_basic response

は私のコントローラです:

class ProjectsController < ApplicationController 
before_filter :http_basic_auth, only: [:show] 

    def http_basic_auth 
    if (authenticate_or_request_with_http_basic do |user, password| password == @project.passcode && user == @project.passcode end if @project.private?) 
    true 
    else 
     response.headers["Location"] = url_for(root_path) 
    end 
    end 

end 

は以下を調査したが、私はまだそれが権利を取得することはできませんよ。
Rails: What is the location option for in the render method

http://api.rubyonrails.org/classes/ActionController/HttpAuthentication/Basic/ControllerMethods.html#method-i-authenticate_with_http_basic

http://guides.rubyonrails.org/v2.3.11/action_controller_overview.html#the-request-and-response-objects

+0

をredirect_to root_path'? – Aleksey

+0

私は試しましたが、うまく動作しませんでした – Asan

+0

それは確かに動作します。 'redirect_to'でない状態で何かが間違っているかもしれません – Aleksey

答えて

1

私はあなたがまだ仕事にこれをもらっていない:authenticate_or_request_with_http_basic

例のスニペットの代わりに

てみ使用authenticate_with_http_basicRails API documentationから:

あなたは `を使用していない理由を
if user = authenticate_with_http_basic { |u, p| @account.users.authenticate(u, p) } 
     @current_user = user 
    else 
     request_http_basic_authentication 
    end 
+0

両方のオプションは動作しますが、ユーザがポップアップウィンドウを閉じるかキャンセルすると、ルートパスにリダイレクトできませんでした。 – Asan