我是这么配置的: get 'doc/share/:randcode_:docid.html' => 'doc#share2'
报错: ActionController::RoutingError (No route matches [GET] "/doc/share/f31f453dd356_147.html"):
如果我把下划线修改为中划线,是支持的。 但是我想用下划线,应该怎么写?
bing 了一下,没有找到答案,所以过来求助,请各位指点一二,多谢!
试试
get 'doc/share/:randcode_:docid.html' => 'doc#share2', :constraints => { :randcode => /\h+/, :docid => /\d+/ }
#1 楼 @Rei 倒是匹配到了,但是参数解析有问题: Parameters: {"randcode_"=>"f31f453dd356_14", "docid"=>"7"}
#1 楼 @Rei \h 是什么 没见过唉
\h
#3 楼 @aisensiy /\h/ - A hexdigit character ([0-9a-fA-F])
#1 楼 @Rei
解决了,加一个括号即可: get 'doc/share/(:randcode)_:docid.html' => 'doc#share2'
get 'doc/share/(:randcode)_:docid.html' => 'doc#share2'
解析结果: Parameters: {"randcode"=>"f31f453dd356", "docid"=>"147"}
#4 楼 @Rei 哦~ cool~
#5 楼 @jeky 这个问题应该是 Rails 把 :randcode_ 当成一个 symbal