我正在根据这里的教程:https://wesleyluyten.com/writing/vanity-urls-for-multiple-controllers-in-rails开发实现虚拟URL
要使虚荣心URL发挥作用,我需要使用这样的机架路由器:
class SlugRouter
def self.to(action)
new(action)
end
def initialize(action)
@action = action
end
def call(env)
params = env['action_dispatch.request.path_parameters']
params[:action] = @action
sluggable = Slug.where('lower(url) = ?', params[:slug].downcase).first
model = sluggable.try(:sluggable_type)
raise ActionController::RoutingError.new('Not Found') if !model
controller = [model.pluralize.camelize,'Controller'].join
params[:controller] = model.pluralize.downcase
controller.constantize.action(params[:action]).call(env)
end
end问题是对于raise ActionController::RoutingError.new('Not Found') if !model,这不是很好地处理路由错误。我如何优雅地处理路由错误,重定向到根目录w,一条闪存消息,而现在发生的是rails显示错误页w
ActionController::RoutingError (Not Found):
lib/slug_router.rb:14:in `call'发布于 2017-01-05 04:24:35
尝试:
redirect_to root_path, notice: 'Unknown Route' if !model或者:
if !model
params[:controller] = 'index'
IndexController.action('index').call(env)
else
...
endhttps://stackoverflow.com/questions/41477238
复制相似问题