[英]How to create multiple links from a single array of strings Rails - Ruby
I know it might seem simple but I've tried to create multiple links from this array in Rails我知道这可能看起来很简单,但我尝试在 Rails 中从这个数组创建多个链接
array = ["/uploads/content/attachment/folder/file1.pdf/file2.pdf/file3.pdf"]
What I want to do is create a link for file1
and another for file2
and so on.我想要做的是为file1
创建一个链接,为file2
创建另一个链接,依此类推。
I've tried to use the join and separate method, image_tag, content_tag and many many different cycles in Rails but every single one ends up like the link above.我尝试在 Rails 中使用连接和分离方法、image_tag、content_tag 和许多不同的循环,但每个循环都像上面的链接一样结束。
Something like this if I understood correctly:如果我理解正确的话是这样的:
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"
The question is not clear.问题不清楚。
I assume the array will be like this:我假设数组将是这样的:
array = [
"/uploads/content/attachment/folder/file1.pdf/file2.pdf/file3.pdf",
"/uploads/content/attachment/folder/file12.pdf/file23.pdf/fildf34.pdf",
"/foo/boo/folder/file1.doc/file2.docx/file11.pdf"
]
It splits links for folder/
它拆分folder/
的链接
links = array.map{ |a| a.split('folder/') }.flat_map do |path, files|
files.split('/').map{ |file| path + "folder/" + file }
end
p links
#=> [
"/uploads/content/attachment/folder/file1.pdf",
"/uploads/content/attachment/folder/file2.pdf",
"/uploads/content/attachment/folder/file3.pdf",
"/uploads/content/attachment/folder/file12.pdf",
"/uploads/content/attachment/folder/file23.pdf",
"/uploads/content/attachment/folder/fildf34.pdf",
"/foo/boo/folder/file1.doc",
"/foo/boo/folder/file2.docx",
"/foo/boo/folder/file11.pdf"
]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.