在我的routes.rb中,我有一些路径,如:
match \action_root_path\action => redirect('\controller_path')
这对所有理智的案件都很有效。但假设我请求的格式如下:
localhost:3000\action_root_path\action.xml
localhost:3000\action_root_path\action.json
我在ApplicationController中处理有效/无效的扩展请求
但是,如果按照路由有重定向,则格式丢失,检查无效,重定向成功。
编辑:
以罗宾斯的回答为线索,我在我的路线上添加了以下内容:
match \action_root_path\action(.format) => redirect('\controller_path.%{format}')
但是要完成这项工作,我还需要添加一个:defaults => {:format => 'html'},因为.已经添加到重定向路径中。
有没有办法让这段时间也是可选的?
更好的是,我能用更短或更好的方式处理这件事吗?
我试着将重定向路径设置为redirect('\controller_path(.%{format})'),但这实际上占用了大括号。
Ruby-1.8.7
Rails - 3.0.2
发布于 2015-11-24 09:07:25
您可以匹配.:format并将其再次添加到重定向路径。
match '/action_root_path/action(.:format)', to: redirect('/controller_path.%{:format}'), defaults: { format: 'html' }如果您只想匹配内部控制器的路径,我建议您这样做:
get '/action_root_path/action', to: 'ApplicationController#yourAction'https://stackoverflow.com/questions/33889129
复制相似问题