gpt4 book ai didi

ruby-on-rails - 自定义404无法从 Controller 触发

转载 作者:行者123 更新时间:2023-12-03 08:31:05 26 4
gpt4 key购买 nike

我尝试使用cms gem时处理404错误。在我以前的应用程序中,此404方法似乎可以工作,但现在无法解决该错误。有任何想法吗?

# 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

和路线
# 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

最佳答案

将路由添加到 Controller 的此操作:

match 'path' to: 'errors#catch_404'

并像这样创建错误 Controller
class ErrorsController < ApplicationController
def catch_404
raise ActionController::RoutingError.new(params[:path])
end
end

之后,它应该开始工作。

关于ruby-on-rails - 自定义404无法从 Controller 触发,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23476699/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com