【问题标题】:How do I create a module/helper to auto select the correct ruby image or video tag?如何创建一个模块/助手来自动选择正确的 ruby​​ 图像或视频标签?
【发布时间】:2019-12-23 20:35:03
【问题描述】:

我正在使用 Active Storage 上传文件。我不想根据 content_type 猜测使用哪个标签来选择活动存储对象。我已经创建了一个案例语句来在一个有效的视图中自动选择,但是为了练习 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】:

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

    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
    

    【讨论】:

    • 我的显示操作 image_tag 需要看起来像这样 &lt;%= image_tag((@board.image.variant(resize: @board.resolution) if @board.image.attached?), :class =&gt; "img-fluid") %&gt; 与索引操作 &lt;%= image_tag((board.image.variant(resize: "300x300") if board.image.attached?), :class =&gt; "card-img-top") %&gt; 所以你的回答只是解决方案的一半。
    • 只需将 action_name 传递给助手并在您的案例语句中执行逻辑。
    • 为了稍微简化一些事情,您不必将 action_name 作为参数传递,因为它已经在帮助程序中可用。
    • 我最终简化了我的 case 语句以使用字符串文字 /image*/ 和 /application*/。考虑到它仅用于显示和索引操作。我想多了 DRY,但这是一个很好的做法。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-05
    • 2022-11-28
    • 2016-04-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多