2016-09-06 106 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)。 –