【发布时间】: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