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

徘徊边缘 提交于 2019-12-10 11:53:59

问题


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" />


来源:https://stackoverflow.com/questions/5136515/ruby-on-rails-generate-html-for-each-file-in-a-folder

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!