首页 > 解决方案 > Rails 查询参数约束?

问题描述

有什么方法可以将约束应用于 Rails 路由中的查询字符串?

有效路线:/path/?type=A/path/?type=B

任何其他类型都应该是无效路由。例如。/path/?type=C应该是无效的(400 Bad request)

标签: ruby-on-railsroutes

解决方案


你可以在你的行动中做到这一点......

def my_action
  raise ActionController::BadRequest unless %w(A B).include?(params[:type])
  ... # normal actions here
end

推荐阅读