Question

I'm trying to handle 404 errors when using a cms gem. In my past application this 404 method seemed to work but now its not rescuing the error. Any ideas?

# application_controller.rb
class ApplicationController < ActionController::Base
  # Prevent CSRF attacks by raising an exception.
  # For APIs, you may want to use :null_session instead.
  protect_from_forgery with: :exception
  rescue_from ActionController::RoutingError, :with => :render_404

  def after_sign_in_path_for(resource)
    admin_cms_path
  end

private

  def render_404(exception = nil)
    if exception
      logger.info "Rendering 404: #{exception.message}"
    end
    flash[:error] = 'The page you entered is unavailble, please send us an email if this is unexpected'
    redirect_to root_path
  end
end

and the routes

# routes.rb
Amskier::Application.routes.draw do
  # ...    
  comfy_route :blog_admin,        path: '/admin'
  comfy_route :cms_admin,         path: '/admin'

  root to: "cms/content#show"

  comfy_route :cms,               path: '/', sitemap: false
end
Was it helpful?

Solution

Add the route to this action of controller:

match 'path' to: 'errors#catch_404'

and create errors controller like this

class ErrorsController < ApplicationController
  def catch_404
    raise ActionController::RoutingError.new(params[:path])
  end
end

after this it should start working.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top