Ruby on rails 错误重定向后不会显示闪存消息
我有一个运行在Rails 3.2上的老式Spree 2.0在线商店,我正试图使用更干净的错误处理来定制它Ruby on rails 错误重定向后不会显示闪存消息,ruby-on-rails,ruby-on-rails-3,spree,Ruby On Rails,Ruby On Rails 3,Spree,我有一个运行在Rails 3.2上的老式Spree 2.0在线商店,我正试图使用更干净的错误处理来定制它 config.exceptions_app = self.routes 在我的config/application.rb中 出于调试的目的,我已转到 config.consider_all_requests_local = false 在my config/environments/development.rb中 在my routes.rb中,我有: match '/404', to:
config.exceptions_app = self.routes
在我的config/application.rb中
出于调试的目的,我已转到
config.consider_all_requests_local = false
在my config/environments/development.rb中
在my routes.rb中,我有:
match '/404', to: 'errors#file_not_found'
在我的控制器中,我有:
def file_not_found
flash[:error] = 'The page you requested could not be found.'
flash.keep
redirect_to :back
rescue ActionController::RedirectBackError
flash[:error] = 'The page you requested could not be found.'
flash.keep
redirect_to '/'
end
我尝试了几种不同的方法,包括在示例中描述的方法,但结果总是,如果我特别转到localhost:3000/404,flash消息有效,但是如果我转到一些不存在的URL,例如localhost:3000/asdfasdf,则flash消息无效
在app/views/spree/layouts中,我有spree_application.html.erb,我有
<%= flash_messages %>
are alert are are alert are are are are are are are alert are WARE SHOW flash[:alert]=找不到您请求的页面。我尝试显示哪种类型的闪存消息没有任何区别。它们只有在我显式键入/404路径时才会显示。您可能需要包含帮助程序在布局中包含此代码段。我想知道它是否与您当前在中间件堆栈中的位置有关。可能是因为flash已经提交,所以您正在进行的任何更改都会被丢弃?可能是因为/404在您直接访问该路线时匹配,所以它不会出现异常。
def flash_messages(opts = {})
opts[:ignore_types] = [:commerce_tracking].concat(Array(opts[:ignore_types]) || [])
flash.each do |msg_type, text|
unless opts[:ignore_types].include?(msg_type)
concat(content_tag :div, text, class: "flash #{msg_type}")
end
end
nil
end