我如何创建一个模块/帮助程序来自动选择正确的红宝石图像或视频标签?

问题描述 投票:0回答:1

我正在使用Active Storage上传文件。我不想不必猜测要基于content_type使用哪个标签来选择活动存储对象。我创建了一个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
ruby-on-rails ruby rails-activestorage
1个回答
0
投票

为什么您不只是将董事会传递给您的助手?

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
© www.soinside.com 2019 - 2024. All rights reserved.