简体   繁体   English

如何从单个字符串数组创建多个链接 Rails - Ruby

[英]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.

相关问题 Ruby on Rails在单个查询中更新多个记录 - Ruby on Rails update multiple records in a single query 如何从SQL Server中的单个行中提取多个字符串 - How to extract multiple strings from single rows in SQL Server 如何在ruby中向SQL查询插入字符串数组 - how to insert an array of strings to sql query in ruby 如何将多个字符串连接到单个SQL查询中? - How to ArrayJoin multiple strings into single SQL query? 如何根据 SQL 中单行中的列的条件创建多行 - How to create multiple rows from conditions on columns in a single row in SQL 如何使用 sql 从雪花中的单行创建多行 - How to create multiple rows from a single row in snowflake using sql 如何从单行创建多行-标准化表 - How to create multiple rows from a single row - normalize a table SQL 如何从单个列中的所有值创建 JSON 数组 - SQL how to create a JSON array from all the values in a single column Rails 3-如何执行一个具有单个异常的find_all_by(从结果数组中删除单个记录)? - Rails 3 - How to do a find_all_by with a single exception (removing a single record from the resulting array)? 从单个数组创建多维数组 - Create multidimensional array from single array
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM