ruby-on-rails - Rails 查询参数约束?
问题描述
有什么方法可以将约束应用于 Rails 路由中的查询字符串?
有效路线:/path/?type=A
和/path/?type=B
任何其他类型都应该是无效路由。例如。/path/?type=C
应该是无效的(400 Bad request)
解决方案
你可以在你的行动中做到这一点......
def my_action
raise ActionController::BadRequest unless %w(A B).include?(params[:type])
... # normal actions here
end
推荐阅读
- sqlite - SQLITE::Statement::execute() 与 SQLITE::Database::exec()
- javascript - 来自内置语言扩展的项目范围的 Javascript/TypeScript 语言错误
- node.js - 将应用程序部署到heroku但应用程序无法启动时,如何解决“Express不是构造函数”类型错误?
- java - Angular - Maven/Spring Boot CORS 不工作
- android - 使用边距和填充时,Android GUI 看起来不同
- javascript - Object.keys.map 仅返回第 n 个值并排除其余值
- c# - MultipartFormDataContent 未在 HttpRequestBody 中添加 Content-Type
- c# - 为什么查询唯一行的 PK 为 0 的非空序列有时会抛出?
- html - 放大网站时如何阻止文字和图像拉伸
- c++ - Widevine 会话更新无限循环