Rails /从link_to中省略?

时间:2010-06-12 17:43:30

标签: ruby-on-rails api url hyperlink

我正在使用Gem通过他们的API与FreeagentCentral进行通信。我有以下代码来显示相关freeagent项目的链接:

<%= link_to "#{image_tag('/images/icons/Spinning Beach Ball.png')} Freeagent Project", "#{Freeagent::Base.site.to_s + Freeagent::Project.element_path(@kase.freeagent_id).gsub(/\A\//, '').gsub!(/.xml/,'')}" if @kase.freeagent_id %>

问题 - 网址中有/省略了这样的网址:

https://XXXXX.freeagentcentral.comprojects/12345

应该是:

https://XXXXX.freeagentcentral.com/projects/12345

这可能很简单,但对我来说 - 这让我发疯了!

谢谢,

丹尼

1 个答案:

答案 0 :(得分:1)

我假设Freeagent::Base.site正在重新调整'https://XXXXX.freeagentcentral.com'。在这种情况下,您只需要在站点和项目部分之间的字符串中添加/。请尝试以下方法:

<%= link_to "#{image_tag('/images/icons/Spinning Beach Ball.png')} Freeagent Project", 
  "#{Freeagent::Base.site}/#{Freeagent::Project.element_path(@kase.freeagent_id).gsub(/\A\//, '').gsub!(/.xml/,'')}" if @kase.freeagent_id %>

编辑:

更密切地看待这一点,gsub(/\A\//, '')正在从/返回的字符串的开头删除Freeagent::Project.element_path(@kase.freeagent_id)。假设element_path确实返回带有前导/的字符串,则更好的答案是删除第一个gsub调用:

<%= link_to "#{image_tag('/images/icons/Spinning Beach Ball.png')} Freeagent Project", 
  "#{Freeagent::Base.site}#{Freeagent::Project.element_path(@kase.freeagent_id).gsub!(/.xml/,'')}" if @kase.freeagent_id %>