Jen Lijó Jen Lijó - 1 month ago 5
Ruby Question

loop through each file using audio_tag Rails 5

I'm new to RoR. I am trying to use audio_tag to reproduce files. My code is reproducing the same song on each audio_tag shown. I would like it to loop through the existed audio files and reproduce them according to its link.

All help will be appreciated!

My code so far:

<% @songs.each do |song| %>
<ul>
<%= link_to song.name, song.url %>
<%= audio_tag(Song.pluck(:url), controls: true) %>
///
<%= link_to "Delete >", "songs/delete/?song=" + song.name, :confirm => 'Are you sure you want to delete ' + song.name + '?' %>
</ul>
<% end %>

Answer

Song.pluck(:url) will return an array of the url of every song in your database.

I think you may want:

<?= audio_tag(song.url, controls: true) %>

This will use the url for the current song object.