我试图解析 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 中手动键入。 拖放似乎不起作用 。