首页 > 解决方案 > 如何从单个字符串数组创建多个链接 Rails - Ruby

问题描述

我知道这可能看起来很简单,但我尝试在 Rails 中从这个数组创建多个链接

array = ["/uploads/content/attachment/folder/file1.pdf/file2.pdf/file3.pdf"]

我想要做的是创建一个链接file1和另一个file2,依此类推。

我尝试在 Rails 中使用连接和分离方法、image_tag、content_tag 和许多不同的循环,但每个循环都像上面的链接一样结束。

标签: sqlruby-on-railsarraysruby

解决方案


如果我理解正确的话是这样的:

array = ["/uploads/content/attachment/folder/file1.pdf/file2.pdf/file3.pdf"]
base = "https://www.example.com/" #the first part of the link, that's same for all links

links = array.first[1..-1].split("/").map{|a| base + a}
puts links
#=>  "https://www.example.com/uploads",
#    "https://www.example.com/content",
#    "https://www.example.com/attachment",
#    "https://www.example.com/folder",
#    "https://www.example.com/file1.pdf",
#    "https://www.example.com/file2.pdf",
#    "https://www.example.com/file3.pdf"

推荐阅读