In this paper, a modern method is presented to solve a class of fractional optimal control problems (FOCPs) indirectly. First, the necessary optimality conditions for the FOCP are obtained in the form of two fractional differential equations (FDEs). Then, the unknown functions are approximated by the hybrid functions, including Bernoulli polynomials and Block-pulse functions based on the spectral Ritz method. Also, two new methods are proposed for calculating the left Caputo fractional derivative and right Riemann-Liouville fractional derivative operators of the hybrid functions that are proportional to the Ritz method. The FOCP is converted into a system of the algebraic equations by applying the fractional derivative operators and collocation method, which determines the solution of the problem. Error estimates for the hybrid function approximation, fractional operators and, the proposed method are provided. Finally, the efficiency of the proposed method and its accuracy in obtaining optimal solutions are shown by some test problems.