我有一个Rails应用程序,它在列表中的一个视图中列出了不同的团队。这些团队是链接并在URL中使用,例如localhost:3000/teams/chelsea
显示“chelsea”。
如果我点击链接时有一个名为man utd
的团队,它会转义所有空格,因此URL就像:
localhost:3000/teams/<script>man%20utd
但我只是希望它是
localhost:3000/teams/<script>man utd
这是我的代码:
<a href="/<%= team["name"].html_safe %>"><%= team["name"] %></a>
我在阅读其他问题之后尝试使用html_safe
,并raw
但是没有运气。
解决此问题的最佳方法是什么?
答案 0 :(得分:0)
我不会在您的网址中添加任何空格 - 请将其作为“man-utd”。见Spaces in URLs?
还有更多Rails-y方式来写出你的链接。如果您的团队是数据库模型,我会将其写为:
<% @teams.each do |team| %>
<%= link_to team.name, team %>
<% end %>
但是这将链接到/ teams / 1和teams / 2等,你必须将特定的路线映射到/ teams / chelsea到/ teams / 1。
如果你刚刚将它们作为man utd和chelsea的单独视图完成,那么就这样做:
<%= link_to 'Man Utd', :controller => 'teams', :action => 'man-utd' %>
<%= link_to 'Chelsea', :controller => 'teams', :action => 'chelsea' %>