2016-09-06 107 views
0

users/_form.html.erb中有一系列利益空间,例如空格。 “漫画超级英雄开心”。当我保存用户的兴趣时,它们会显示在users/show.html.erb文件中,如同附加图像中的一个长链接。如何使用拆分或连接来拆分标记链接

如何区分标签链接?

我想这个代码分割的联系,但没有成功:

<%= raw @user.tag_list.map { |t| link_to t, tag_path(t) }.join(" ") %> 

>> @user.tag_list 
=> ["comic superhero happy"] 

trying to get this result: 
>> @user.tag_list 
=> ["comic" "superhero" "happy"] 

enter image description here

+0

它看起来像问题是,'@ user.tag_list'返回一个字符串。显示该方法的代码。另外,你展示的视图代码放到HTML中是什么? –

回答

1
tags = @user.tag_list.split(" ").map do |t| 
    link_to t, tag_path(t) 
end 
+0

谢谢ruby_newbie – ARTLoe

+1

没有必要使用'split(“”)'。使用'split'会做同样的事情。这是[文档](http://ruby-doc.org/core-2.3.1/String.html#method-i-split)。 –