Ruby on Rails: How to change RecordNotFound behavior?

When you go to the show object page with an identifier that does not exist, a RecordNotFonud exception is RecordNotFonud . Is there a way that I can redirect to a page with an error, or perhaps another action for this error?

+4
source share
6 answers

You can use rescue_from if you use Rails 3:

 class ApplicationController < ActionController::Base rescue_from ActiveRecord::RecordNotFound, :with => :render_404 def render_404 respond_to do |format| format.html { render :action => "errors/404.html.erb", :status => 404 } # and so on.. end end end 

Yes, you can also do a redirect instead of a render, but this is not a good idea. Any semi-automatic interaction with your site will assume that the transfer was successful (because the return code was not 404), but the received resource was not the one you need.

+10
source

In development mode, you will see information about the exception, but it should automatically display the 404.html file from your shared directory when your application is running.

+1
source

See http://api.rubyonrails.org/classes/ActiveSupport/Rescuable/ClassMethods.html . Rails has nice features for handling exceptions.

+1
source

I usually do something like this in my ApplicationController

 class ApplicationController < ActionController::Base rescue_from ActiveRecord::RecordNotFound, :with => :routing_error private def routing_error redirect_to(root_url, :alert => "Sorry, the page you requested could not be found.") end end 
+1
source

If you need to handle a few specific exceptions, use rescue_action or rescue_action_in_public , the difference in binding local requests or not (general development / production). I prefer to use in_public because you need to view the backtrace exception in design mode.

take a look at my source code:

 class ApplicationController < ActionController::Base include CustomExceptionsHandler .... end module CustomExceptionsHandler # Redirect to login/dashboard path when Exception is caught def rescue_action_in_public(exception) logger.error("\n !!! Exception !!! \n #{exception.message} \n") case exception.class.to_s when "Task::AccessDenied" logger.error(" !!! 403 !!!") notify_hoptoad(exception) //catch this kind of notification to Hoptoad render_403 when "AuthenticatedSystem::PermissionDenied" logger.error(" !!! 403 !!!") render_403 when "Task::MissingDenied" logger.error(" !!! 404 !!!") notify_hoptoad(exception) render_404 when "ActionController::RoutingError" logger.error(" !!! 404 !!!") render_404 else notify_hoptoad(exception) redirect_to(current_user.nil? ? login_path : dashboard_path) and return false end end private #403 Forbidden def render_403 respond_to do |format| format.html { render :template => "common/403", :layout => false, :status => 403 } format.xml { head 403 } format.js { head 403 } format.json { head 403 } end return false end #404 Not Found def render_404 respond_to do |format| format.html { render :template => "common/404", :layout => false, :status => 404 } format.xml { head 404 } format.js { head 404 } format.json { head 404 } end return false end end 
0
source

Use the begin-rescue-end construct to catch the exception and do something useful with it.

 userid=2 begin u=User.find userid rescue RecordNotFound redirect_to "/errorpage" #Go to erropage if you didn't find the record exit end redirect_to u # Go to the user page 
-1
source

Source: https://habr.com/ru/post/1369667/


All Articles