ruby-on-rails 使用帮助器方法时不显示SVG

wgeznvg7  于 2023-10-21  发布在  Ruby
关注(0)|答案(1)|浏览(132)

有什么想法吗,为什么在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 %>

渲染(如预期)

而如果我尝试使用一个helper方法来呈现相同的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),但问题仍然存在。

gdrx4gfi

gdrx4gfi1#

我有一个类似的方法来渲染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

相关问题