首页 > 解决方案 > 将音频播放器添加到活动管理面板

问题描述

我有一个博客,它接受带有 Rails 6 附带的音频文件的评论。

是否可以在活动的管理仪表板中显示音频并收听它们?

我有

#/models/comment.rb
class Comment < ApplicationRecord
  belongs_to :post
  belongs_to :user

  has_one_attached :audio_file
end
#/admin/comments.rb
ActiveAdmin.register Comment do
  actions :all, except: [:edit, :new]
  permit_params :audio_file, :post_id, :created_at, :deleted_by
end

我尝试添加

  index do
    selectable_column
    id_column
    column :audio_file
    column :challenge_id
    column :created_at
    column :deleted_by
    actions
  end

但它只是添加文本audio_file

如何添加audio_tag附件audio_file

标签: ruby-on-railsactiveadmin

解决方案


尝试使用 rails audio_taghelper ( https://apidock.com/rails/ActionView/Helpers/AssetTagHelper/audio_tag ) 显示音频标签,如下所示。

index do
    selectable_column
    id_column
    column :audio_file do |comment|
      if comment.audio_file.attached?
        audio_tag(comment.audio_file, autoplay: true, controls: true)
      else
        # Do whatever needed
      end
    end
    column :challenge_id
    column :created_at
    column :deleted_by
    actions
  end

推荐阅读