Ruby on Rails: Generate HTML for each file in a fo

2019-06-01 06:52发布

问题:

I currently have a folder with a list of small *.ogg files that I would like to insert into an html.erb page in my /public folder.

The code is within video tags - I want to use Ruby to scan the folder and produce a video snippet for each video it finds in the folder. The code will stay the same between videos, except for the video filename.

The resulting page will be a list of playable videos.

Any advice would be awesome.

回答1:

# assumes the current dir contains the ogg files
html = ''
Dir.glob("*.ogg") do |file|
  html += "<a href=\"#{file}\" />"
end
puts html
# <a href="a.ogg" /><a href="b.ogg" />