我正在使用Ruby构建一个简单的面包屑导航,但是我不确定如何实现我的逻辑。
假设我有一个单词数组,这些单词来自我的request.path.split("/)
["", "products", "women", "dresses"]
我想把这些字符串推入另一个数组中,最终得到["/", "/products", "products/women", "products/women/dresses"]
,并将其用作我的面包屑解决方案。
虽然我不擅长Ruby,但我目前想到了以下代码:
cur_path = request.path.split('/')
cur_path.each do |link|
arr = []
final_link = '/'+ link
if cur_path.find_index(link) > 1
# add all the previous array items with the exception of the index 0
else
arr.push(final_link)
end
end
结果应该是["/", "/products", "/products/women", "/products/women/dresses"]