블록 시작 부분에 authenticate_or_request_with_http_basic
에 대한 호출을 추가 할 때 작동하는 컨트롤러 작동이 작동합니다.AbstractController :: DoubleRenderError를 사용할 때 authenticate_or_request_with_http_basic()
def index
authenticate_or_request_with_http_basic do |username, password|
username == "test1" and password == "test"
end
render layout: false, content_type: 'application/xml'
end
나는 다음과 같은 메시지가 브라우저 창에 "AbstractController에 :: DoubleRenderError"오류 응답 수신하고 있습니다 :
가 렌더링 및/또는 리디렉션을 불렸다 여기에 작동하지 않는 액션 코드는 이 동작에서 여러 번 렌더링 또는 리디렉션을 호출 할 수 있으며 액션 당 최대 한 번만 호출 할 수 있습니다. 또한 리다이렉트도 렌더링도 액션의 실행을 종료시키지 않으므로 리다이렉션 후 액션을 종료하려면 "redirect_to (...) and return"과 같은 작업을 수행해야합니다. 내가 별도의 행동에서 "authenticate_or_request_with_http_basic"논리 위치하게 한 후, 인덱스 액션을 실행하기 위해 before_filter를 구성 할 때
는, 모두 잘 있습니다. 그러나, 나는 인덱스 이외의 모든 작업에 대해이 논리를 재사용하지 않으며 위의 코드가 작동하지 않는 이유를 알고 싶습니다.
솔루션 난 타일러와 RKB의 도움으로 해결책을 찾을 수 있었다. 여기있다 : 인증이 성공하면
authenticated = authenticate_or_request_with_http_basic "Authentication Required" do |username, password|
@user = User.find_by_username(username)
@user != nil && password == "test"
end
return unless authenticated == true
authenticate_or_request_with_http_basic
"TRUE"를 반환. 실패하면 401을 반환합니다.
그런 경우 authenticate_or_request_with_http_basic이 false를 반환하면 작업의 이후 실행을 어떻게 중지합니까? –
나는 before_filter에 authenticate_or_request_with_http_basic 호출을 넣을 것이다. – rkb
단일 작업 내에서 추가 실행이 어떻게 중지됩니까? –