컨트롤러 호출로 트리거 된 다음 작업을 수행하는 데 사용되는 코드가 있습니다. 레일스 트랜잭션을 시작/복구 엔드 블록과 함께 사용하여 아래 시리즈를 만듭니다. 같은 더 robust.Its 코드 실행은 다음과 같습니다 :레일 4 - 함께 트랜잭션 사용하기 복구 블록
- 저장을
- 속성 값을 연관을 저장하고 업데이트 할 객체를
- 보내기 이메일
- 보내는 문자 메시지
는
###in my controller def verify @request=Request.includes(:user).find(params[:id]) @request.transaction do begin if @request.accepted? ##this method will call 3 more methods in the request model @request.send_email_and_sms flash[:success] = "Request is moved to another state" else flash[:error] = "Request was not accepted and couldn't be moved to another state" end rescue flash[:alert] = "There was some internal error.Kindly debug" ensure @request.reload Rails.logger.info "================Making GO AHEAD TOKEN FALSE AND DEACTIVATE VENUE====================#{@request.attributes}===========" redirect_to request_hall_path(@request.user) end end end
`
코드의 모든 조각이는 flash.alert 메시지 구조에 이동합니다 다른 실행할 수 있도록하기 위해이 올바른 방법인가 리디렉션합니다.
이 코드/로직을 더 어리석은 증거로 만들기 위해 누락 된 다른 방법이 있습니까?
미리 감사드립니다.
request.update(email_sent: true)
희망처럼 할 수 있도록 그래서 콜백을 등록합니다. 전자 메일 및 SMS 배달은 일시적으로 실패 할 수 있으며 데이터베이스 변경 내용을 롤백해서는 안됩니다. 대신 실패한 작업을 재 시도 할 작업 큐를 사용해야합니다. – meagar
고마워요 @ meagar, 만약 내가 이메일/SMS를 보내고 그들을 처리 async..is이 코드는 여전히 강력하거나 수정이 필요 제거 제거 할 수 있습니까? – Milind