2013-12-20 2 views
0

컨트롤러 레벨에서 삭제하려는 역할 == 관리자 인 경우 사용자가 해당 역할을 삭제하지 못하도록하고 싶습니다.이 레코드를 삭제할 수 없습니다. 레일즈 4.0.2

역할을 삭제하기위한 삭제 링크를 제거 할 수는 있지만 컨트롤러 또는 모델 수준에서는 도움이되지 않습니다.

무슨

답변

1

당신은 before_filter으로이 작업을 수행 할 수 레일 (4.0.x의)이 처리 할 수있는 방법. A before_filter이 조치 전에 실행됩니다.

그래서 컨트롤러에 당신은 추가 할 수 있습니다

before_filter :ensure_not_administrator, only: :destroy 

#your actions go here 

private 

def ensure_not_administator 
    @record = Model.find(params[:id]) 
    if @record.role == 'Administrator' 
    flash[:error] = "Cannot delete this record" 
    redirect_to where_you_want_to_go_path 
    return false 
    end 
end 
관련 문제