我试图将此返回语句设置为内联样式,但我输入的 HTML 只是显示在页面上。我将如何设置此内联样式?
def instruction
if self.o_connection.dongle?
return "Plug the #{self.o_cord.name} end of the <div class='greentext'>#{self.o_product.full_name}</div> into the #{self.i_product.full_name}."
最佳答案
这个方法似乎不在 Helper 中(它可能应该在)。无论如何,您可以使用 html_safe
。
def instruction
if self.o_connection.dongle?
return "Plug the #{self.o_cord.name} end of the <div class='greentext'>#{self.o_product.full_name}</div> into the #{self.i_product.full_name}.".html_safe
另一种方法是在你的 View 中做这样的事情:
<%= raw(object.instruction) %>
无论哪种情况 - 我都会注意任何安全风险。看看Sanitize Helper .
关于css - 我将如何设置此 rails return 语句的内联样式?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18214237/