Home > Software engineering >  Rails 7 rendering to a VTT, displays entity code instead of character
Rails 7 rendering to a VTT, displays entity code instead of character

Time:06-20

I am working on putting captions for videos into a database interface, so the information needs to render into a VTT file for HTML5 tags to be able to display closed captions.

in my controller, the show method:

def show
        @track = Track.find(params[:id])
        @cues = @track.track_cues
        @dash = " --> "

        render template: 'tracks/show', layout: 'tracks', formats: [:vtt]

    end

so in my show.vtt.haml file, I have:

WEBVTT

= print "\n"

- @cues.each do |cue|
    = cue.identifier
    = cue.start_time.strftime("%H:%M:%S.%L")   @dash   cue.end_time.strftime("%H:%M:%S.%L")

    = print "\n"

Which currently renders as the following screenshot Screenshot showing a WebVTT rendering where the dash between the start and end time uses a entity code

Instead, it SHOULD be rendering like thus:

WEBVTT

1
0:00:00.000 --> 0:00:03.000 line:90%

2
0:00:03.000 --> 0:00:06.000 line:90%

I'm not sure what I'm doing wrong here that has it rendering the entity code instead of the greater than character.

CodePudding user response:

Rails escapes everything in the view by default. However, you can circumvent sanitisation by using raw:

= raw(cue.start_time.strftime("%H:%M:%S.%L")   @dash   cue.end_time.strftime("%H:%M:%S.%L"))

or, haml-specific, the != operator has the same effect as prepending raw:

!= cue.start_time.strftime("%H:%M:%S.%L")   @dash   cue.end_time.strftime("%H:%M:%S.%L")

(In ERB, <%== %> would have the equivalent effect.)

  • Related