简体   繁体   English

使用font-awesome-rails与HAML文件时遇到问题

[英]Trouble using font-awesome-rails with a HAML file

I am currently trying to change from an existing svg file in the following section of a HAML file: 我目前正在尝试从HAML文件以下部分中的现有svg文件进行更改:

.pokecon-btn-search-lg
   %i.btn.w100= image_tag "icons/search_white.svg", class: "pokecon-img-icon-search"
   %input.pokecon-btn-search-lg-submit{type: "submit", value: ""}

The goal is to switch over to the font-awesome search icon which is usually called into HTML by 目的是切换到超棒的搜索图标,通常通过以下方式将其称为HTML

<i class="fa fa-search" aria-hidden="true"></i>

The gem font-awesome-rails is installed, and called into application.css.scss via @import "font-awesome"; gem font-awesome-rails已安装,并通过@import "font-awesome";调用到application.css.scss @import "font-awesome";

In order to start get the line into the HAML file, I was advised to create an image helper like so: 为了开始将该行放入HAML文件,建议我创建一个图像帮助器,如下所示:

module  ImagesHelper
  def fa(name)
    return "<i class='fa #{name}' aria-hidden='true'></i>"
  end
end

This allowed me to change the HAML file into: 这使我可以将HAML文件更改为:

%i.btn.w100
  = fa 'fa-search pokecon-img-icon-search'
  %input.pokecon-btn-search-lg-submit{type: "submit", value: ""}

Unfortunately, this is just rendering the text <i class="fa fa-search pokecon-img-icon-search" aria-hidden="true"></i> on top of the button instead of the actual icon that is needed. 不幸的是,这只是在按钮顶部而不是所需的实际图标上呈现文本<i class="fa fa-search pokecon-img-icon-search" aria-hidden="true"></i> I have tried restarting the rails server and running bundle install again just in case, but the issue persists. 我试图重新启动Rails服务器并再次运行捆绑安装,以防万一,但是问题仍然存在。

I am not super confident with HAML files so I would not be surprised if I am heading in completely the wrong direction with things here! 我对HAML文件不是很自信,所以如果我对这里的事情完全走错了方向,我不会感到惊讶!

Try it without the helper first, then work out how to get the HAML into the helper method later. 首先在没有帮助程序的情况下进行尝试,然后再找出如何将HAML放入帮助程序方法。

This ought to work: 这应该起作用:

%i.btn.w100
  .fa.fa-search.pokecon-img-icon-search{ "aria-hidden" => true }
  %input.pokecon-btn-search-lg-submit{ type: "submit", value: "" }

The string being returned by your helper isn't marked as html_safe you can fix this by using: 您的帮助程序返回的字符串未标记为html_safe您可以使用以下方法解决此问题:

return "<i class='fa #{name}' aria-hidden='true'></i>".html_safe

but, then if name can be modified by a user they can inject whatever they want into that string. 但是,如果用户可以修改name ,则可以将所需的内容注入该字符串中。 (Try calling it with <%= fa('\\'"><script>alert("Hello");</script>') %> ) Instead I would do something like (尝试使用<%= fa('\\'"><script>alert("Hello");</script>') %>调用它)相反,我会做类似的事情

content_tag :i, '', 'class' => ['fa', name], 'aria-hidden' => true

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM