我正在尝试解析 URL。例如,我试图退出的地方:
~/locations/1 => [locations,1]
~/locations/1/comments => [locations,1]
~/locations/1/comments/22 => [locations,1]
~/locations/1/buildings/3 => [buildings,3]
~/locations/1/buildings/3/comments => [buildings,3]
~/locations/1/buildings/3/comments/34 => [buildings,3]
格式相当一致。我从数组开始,但似乎仍然失败:
@request_path = request.path.downcase.split('/')
@comment_index = @request_path.index("comments").to_i
if @comment_index > 0
@request_path = @request_path.drop_while { |i| i.to_i >= @comment_index }
end
resource, id = @request_path.last(2)
我添加了小写字母,以防有人手动输入大写 URL。drop_while 似乎不起作用。