使用辅助方法时不显示 SVG

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

有什么想法为什么在 Rails 7.1 中使用以下代码

 <%= button_tag type: "submit", class: "inline-flex items-center justify-center gap-x-2 rounded-md bg-black px-3.5 py-2.5 w-full sm:w-min text-sm font-semibold text-zinc-200 hover:text-white focus-visible:outline focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-black" do %>
  <span class="first-letter:capitalize"><%=t ("save") %></span>
  <svg class="h-6 w-6" viewBox="0 0 24 24" stroke-width="1.5" fill="currentColor">
    <path fill-rule="evenodd" d="M2.25 12c0-5.385 4.365-9.75 9.75-9.75s9.75 4.365 9.75 9.75-4.365 9.75-9.75 9.75S2.25 17.385 2.25 12zm13.36-1.814a.75.75 0 10-1.22-.872l-3.236 4.53L9.53 12.22a.75.75 0 00-1.06 1.06l2.25 2.25a.75.75 0 001.14-.094l3.75-5.25z" clip-rule="evenodd" />
  </svg>
<% end %>

渲染(如预期)

同时,如果我尝试使用辅助方法渲染相同的 SVG

 <%= button_tag type: "submit", class: "inline-flex items-center justify-center gap-x-2 rounded-md bg-black px-3.5 py-2.5 w-full sm:w-min text-sm font-semibold text-zinc-200 hover:text-white focus-visible:outline focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-black" do %>
  <span class="first-letter:capitalize"><%=t ("save") %></span>
  <%= svg "check-circle-solid" %>
<% end %>
module ApplicationHelper
  def svg(name)
    file = Rails.root.join("app", "assets", "images", "#{name}.svg")
    File.read(file).html_safe
  end
end

它呈现

即使检查的代码看起来像

我尝试了一些浏览器(Firefox、Chrome、Safari),但问题仍然存在。

ruby-on-rails svg assets
1个回答
0
投票

我有一个类似的方法来渲染 svg,它采用存储在资产文件夹中的 svg 的路径...

## can be used as embedded_svg("kiwi.svg")
def embedded_svg filename, options={}
   file = File.read(Rails.root.join('app', 'assets', 'images', 'svgs', filename))
   doc = Nokogiri::HTML::DocumentFragment.parse file
   svg = doc.at_css 'svg'
   if options[:class].present?
     svg['class'] = options[:class]
   end
   doc.to_html.html_safe
 end
© www.soinside.com 2019 - 2024. All rights reserved.