Rails — 带有“点"的参数(例如/google.com) [英] Rails — Params with "dot" (e.g. /google.com)

查看:49
本文介绍了Rails — 带有“点"的参数(例如/google.com)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何强制 Rails 将值中带有点的参数(例如 google.com(例如 /some_action/google.com)视为单个参数而不是 <代码>id"=>谷歌",格式"=>com"?

How to force Rails to consider a param with a dot in the value like google.com (e.g. /some_action/google.com) a single param and not "id" => "google", "format"=> "com"?

参数值应该是 "id" =>google.com"

推荐答案

默认情况下,动态段不接受点 - 这是因为点被用作格式化路由的分隔符.但是,您可以向路由参数添加一些正则表达式要求.在这里,您要允许参数中的点.

By default, dynamic segments don't accept dots - this is because the dot is used as a separator for formatted routes. However, you can add some regex requirements to the route parameters. Here, you want to allow the dots in the parameters.

match 'some_action/:id' => 'controller#action', :constraints  => { :id => /[0-z\.]+/ }

在 rails 2.3 中:

And in rails 2.3:

map.connect 'some_action/:id', :controller => 'controller', :action => 'action',  :requirements => { :id => /[0-z\.]+/ } 

相关 rails 指南部分

这篇关于Rails — 带有“点"的参数(例如/google.com)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆