我在Rails中使用link_to和to_sentence时遇到了一些问题。
使用此行代码,html输出是错误的。
<%= @story.collections.map{ |u| link_to(u.name, collection_path(u)).html_safe }.to_sentence %>此代码在页面上呈现为文本,而不是链接
<a href="/collections/One">One</a>, <a href="/collections/Two">Two</a>, and <a href="/collections/Three">Three</a>这就是我想要的
One、Two和Three
感谢您的帮助,谢谢!
发布于 2017-05-22 23:54:13
使用safe join作为助手方法,如下所示:
在helper中:
def story_links(story)
links = @story.collections.map{ |u| link_to(u.name, collection_path(u)).html_safe }
safe_join(links, ',')
end在视图中:
<%= story_links(@story) %>https://stackoverflow.com/questions/44103830
复制相似问题