1

http://myside.com/MyControllers/213123123 gives me Couldn't find MyController with id=213123123

def show
  render :text=>'testing ....'  
  begin
    @ingredient = MyController.find(params[:id])
  rescue RecordNotFound => e
    puts "Test........"
  end
  respond_to do |format|
    format.html # show.html.erb
    format.json { render json: @ingredient }
  end
end

render :text=>'testing ....' is not rendring if invalid id given Couldn't find MyController with id=213123123

Any one guide me what will be the right syntax

Mandeep
  • 9,093
  • 2
  • 26
  • 36
xrock
  • 35
  • 6

1 Answers1

0

You've got several important syntax issues:

  1. Your routes seem malformed (you need to use snake_case rather than CamelCase)
  2. You'll be better using the exception_app middleware hook to capture the exception

I would fix these as follows:

#config/routes.rb
resources :my_controllers #-> domain.com/my_controllers/:id

#app/controllers/my_controllers_controller.rb
Class MyController < ApplicationController
   def show
       @ingredient = MyController.find params[:id]
   end
end

exceptions_app

I've actually written extensively about this here & written a gem here & there is a great github gist here

This will then give you the ability to capture any exceptions using the exceptions_app middleware hook:

#config/application.rb
config.exceptions_app = ->(env) { ExceptionController.action(:show).call(env) }

This will allow you to route any exceptions through a separate controller called ExceptionController:

#app/controllers/exception_controller.rb
Class ExceptionController < ApplicationController
   layout 'application'

   def show
     @exception       = env['action_dispatch.exception']
     @status_code     = ActionDispatch::ExceptionWrapper.new(env, @exception).status_code
     @rescue_response = ActionDispatch::ExceptionWrapper.rescue_responses[@exception.class.name]

     respond_to do |format|
       format.html { render :show, status: @status_code, layout: !request.xhr? }
       format.xml  { render xml: details, root: "error", status: @status_code }
       format.json { render json: {error: details}, status: @status_code }
     end
  end
end

#app/views/exception/show.html.erb
Put what you want here

This will help you capture the exceptions - if you'd like to test in development, you should do this:

#config/environments/development.rb
config.consider_all_requests_local = false
Community
  • 1
  • 1
Richard Peck
  • 76,116
  • 9
  • 93
  • 147