I am beginner in ruby and cant find solution how to create custom 404-401 page in Rails 5. Any suggestion? I've created a controller "ErrorPages" with action "page_404". Help me please.
Asked
Active
Viewed 1,393 times
1 Answers
4
Try this:
class ApplicationController < ActionController::Base
rescue_from ActiveRecord::RecordNotFound, with: :on_record_not_found
rescue_from AbstractController::ActionNotFound, with: :on_record_not_found
rescue_from ActionController::RoutingError, with: :on_routing_error
rescue_from CanCan::AccessDenied, with: :on_access_denied
def render_404
if params[:format].present? && params[:format] != 'html'
head status: 404
else
render 'application/404', status: 404
end
end
def on_access_denied
if params[:format].present? && params[:format] != 'html'
head status: 401
else
render 'application/401', status: 401
end
end
def on_record_not_found
render_404
end
def on_routing_error
render_404
end
end
routes.rb
Rails.application.routes.draw do
get '*unmatched_route', :to => 'application#render_404'
end
/app/views/application/404.html.slim
.content
.row
.col-md-12
h1 404

gogaz
- 2,323
- 2
- 23
- 31

Serhii Danovskyi
- 385
- 3
- 17
-
less than a minute ? .... Seems like you pasted code from your project as it is... – Deepak Mahakale Mar 17 '17 at 10:21
-
1Do you own both of these accounts? – Deepak Mahakale Mar 17 '17 at 10:22
-
1I just keep these questions in https://www.evernote.com for example https://www.evernote.com/shard/s554/sh/5b04e531-758c-45b6-8f28-1a9a53fd3e6c/97bd855e98482cd808a5ba7ea46fd2e5 – Serhii Danovskyi Mar 17 '17 at 10:28
-
301 redirect https://www.evernote.com/shard/s554/sh/e3633d93-2e5c-4ef9-8d2c-89b7ad442766/23e9770a5ef2e4f3ea00129da78368b7 – Serhii Danovskyi Mar 17 '17 at 10:31