ruby-on-rails - 路由约束:没有匹配时引发异常
问题描述
给定这样的 Rails 路由约束:
class UserConstraint
def matches?(request)
User.where(code: request.path_parameters[:code]).any?
end
end
由于子路由,这将不起作用。
路线.rb:
constraints UserConstraint.new do
get ':code', to: 'documents#index', as: :documents
get ':code/*slug', to: 'documents#show', as: :document
end
它只返回以下内容:
ActiveRecord::RecordNotFound:
Couldn't find User with 'slug'={:code=>"show"}
这只能通过更多的约束来解决吗?
解决方案
将该查询分配给一个变量并执行类似的操作-
raise <ExceptionClass> unless variable
推荐阅读
- json - 从 Flutter 中的本地 json 文件获取数据
- postgresql - 无法在高山图像上使用 PostGIS
- visual-c++ - 如何从托管 C# 代码跟踪 CRT 调试内存泄漏输出的来源?
- python - 从字符串python中提取单词并使用nlp将它们添加到数组中
- wpf - Menu 中 MenuItems 前后的空格
- php - 在 Laravel 中分页之前获取所有结果 id
- java - Spring Boot - 无法使用 thymeleaf 邮件模板添加内联图像
- r - R:使用填充和 facet_wrap 在图形上添加观察数
- c++ - 修改最小堆中的元素并在亚线性时间内重建堆
- node.js - 如何从Nodejs中的函数中提取变量