rails: handling errors for routes / controllers not found

Create a controller that will handle errors:

class ErrorsController < ApplicationController

  def routing



  def render_404

    render :json => {:status => “error”, :response_code => 404, :message => “no route found that match your request, please check your parameters and try again”, :results => {} }.to_json



And then, in your router, include the following line (at the end, after all the resources are listed, so you can catch stuff in there):

  resources :some_resource_here

  # catch any unmatch paths:

  match ‘*a’, :to => ‘errors#routing’

another option

you can redirect to another page inside your controller, like this:

before_action :set_project, only: [:show, :edit, :update, :destroy]

def set_project
   @project = Project.find(params[:id])
   rescue ActiveRecord::RecordNotFound flash[:alert] = "The  project you were looking for could not be found."
   redirect_to projects_path

Leave a comment

Your email address will not be published. Required fields are marked *