mercoledì 4 dicembre 2013

Rails 4 rescue_from Routing Error path sconosciuti

Per prima cosa in fondo a route.rb

# routes.rb 
# ultima istruzione del file altrimenti naconde quelle precendenti
match '*path', via: :all, to: "application#routing_error"

in questo modo redirigiamo tutti i routing sconosciuti

Quindi  andiamo a generare l'errore che gestiremo come meglio ci aggrada

# application_controller.rb 

def routing_error
  raise ActionController::RoutingError.new(params[:path])

end

rescue_from ActionController::RoutingError, with: :routing_error_rescue

private

def routing_error_rescue
  flash[:error] = "Impossible #{params[:path]}."
  redirect_to request.headers["Referer"] || root_path

end

Fatto