如何创建 module/helper 以自动 select 正确的 ruby 图片或视频标签?

How do I create a module/helper to auto select the correct ruby image or video tag?

我正在使用 Active Storage 上传文件。我不想根据 content_type 猜测要对 select 活动存储对象使用哪个标签。我已经在一个有效的视图中创建了一个自动 select 的 case 语句,但为了练习 DRY,我想将它分解成一个助手或方法。

原始查看代码:

<% case @board.image.content_type
  when "image/png" %>
    <%= image_tag((@board.image.variant(resize: @board.resolution) if @board.image.attached?),  :class => "img-fluid") %>
  <% when "image/jpeg" %>
    <%= image_tag((@board.image.variant(resize: @board.resolution) if @board.image.attached?),  :class => "img-fluid") %>
  <% when "image/gif" %>
    <%= image_tag((@board.image.variant(resize: @board.resolution) if @board.image.attached?),  :class => "img-fluid") %>
  <% when "video/mp4" %>
    <%= video_tag(url_for(@board.image),controls: false, autoplay: true, muted: true, loop: true, class:"vid-fluid")  %>
  <% when "application/pdf" %>
    <%= image_tag @board.image.blob.preview(resize: "1920x1080"), :class => "img-fluid" %>
  <% when "application/msword" %>
    <%= image_tag @board.image.blob.preview(resize: @board.resolution), :class => "img-fluid" %>
  <% when "application/vnd.openxmlformats-officedocument.wordprocessingml.document" %>
    <%= image_tag @board.image.blob.preview(resize: @board.resolution), :class => "img-fluid" %>
  <% when "application/vnd.ms-excel" %>
    <%= image_tag @board.image.blob.preview(resize: @board.resolution), :class => "img-fluid" %>
  <% when "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" %>
    <%= image_tag @board.image.blob.preview(resize: @board.resolution), :class => "img-fluid" %>
  <% when "application/vnd.ms-powerpoint" %>
    <%= image_tag @board.image.blob.preview(resize: @board.resolution), :class => "img-fluid" %>
  <% when "application/vnd.openxmlformats-officedocument.presentationml.presentation" %>
    <%= image_tag @board.image.blob.preview(resize: @board.resolution), :class => "img-fluid" %>
  <% else %>
    <%= link_to 'Back', boards_path %>
<% end %>

这是我创建助手的尝试,但我不知道如何传递标签所需的其他参数。

module BoardsHelper
  def tag_selector(content_type)

    <% case content_type
      when "image/png" %>
        <%= image_tag %>
      <% when "image/jpeg" %>
        <%= image_tag %>
      <% when "image/gif" %>
        <%= image_tag %>
      <% when "video/mp4" %>
        <%= video_tag %>
      <% when "application/pdf" %>
        <%= image_tag %>
      <% when "application/msword" %>
        <%= image_tag %>
      <% when "application/vnd.openxmlformats-officedocument.wordprocessingml.document" %>
        <%= image_tag %>
      <% when "application/vnd.ms-excel" %>
        <%= image_tag %>
      <% when "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" %>
        <%= image_tag %>
      <% when "application/vnd.ms-powerpoint" %>
        <%= image_tag %>
      <% when "application/vnd.openxmlformats-officedocument.presentationml.presentation" %>
        <%= image_tag %>
      <% else %>
        <%= image_tag %>
    <% end %>
  end

end

你为什么不直接将板子传递给你的助手?

module BoardsHelper
  def tag_selector(board, action_name)
    size = action_name == 'index' ? '300x300' : board.resolution
    case board.image.content_type
    when "image/png", "image/jpeg", "image/gif"
      image_tag((board.image.variant(resize: size) if board.image.attached?),  class: "img-fluid")
      ....
    end
  end
end