本文介绍了如何在 .map 块中使用 rails link_to 助手,以便输出是实际链接,而不是 HTML 的文本表示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有这个:
listed in <%= @product.categories.map{ |cat| raw(link_to(cat.name, category_path(cat))) }.join(', ') + "." %>
这是输出:
listed in <a href="/categories/1">Men</a>, <a href="/categories/2">Women</a>.
它实际上并没有将其转换为实际链接.它以网页上的文字形式吐出.
It doesn't actually convert that to an actual link. It spits it out as text on the webpage.
如何让 link_to
帮助程序从该块中显示为链接,而不是 HTML?
How do I get that link_to
helper to be displayed as a link, and not HTML, from within that block?
推荐答案
您需要用 raw
包装 map
调用的结果.这样你就可以告诉 rails 字符串应该直接输出到模板中.
you need to wrap the result of your map
call with raw
. This way you can tell rails that the string should be outputted directly into the template.
listed in <%= raw(@product.categories.map{ |cat| raw(link_to(cat.name, category_path(cat))) }.join(', ') + ".") %>
这篇关于如何在 .map 块中使用 rails link_to 助手,以便输出是实际链接,而不是 HTML 的文本表示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!