I have an app with user and events. Each user has several events. When a user wants to see a specific event he will get to this action:
def show
begin
@use
In application controller, please write:
rescue_from (ActiveRecord::RecordNotFound) { |exception| handle_exception(exception, 404) }
protected
def handle_exception(ex, status)
render_error(ex, status)
logger.error ex
end
def render_error(ex, status)
@status_code = status
respond_to do |format|
format.html { render :template => "error", :status => status }
format.all { render :nothing => true, :status => status }
end
end
Create a page error.html.erb
<%= t "errors.#{@status_code}.heading" %>
<%= t "errors.#{@status_code}.subheading" %>
<%= t "errors.#{@status_code}.description" %>
<% if defined? root_path %>
<%= link_to t(:return_to_home), root_path %>
<% end %>
and in en.yml
en:
errors:
"404":
description: "The page you are looking for does not exist!"
heading: "Record not found"
subheading: ""