Rails默认图像绝对URL

时间:2012-09-07 14:36:12

标签: ruby-on-rails-3

我有一个新手问题:

我将默认URL(指向存储在服务器上的图像)存储为模型的默认图像,例如/assets/project_default.png。用户也可以将URL设置为远程URL。

如何将网址显示为绝对网址,使其为http://xxxxxx.com//assets/project_default.png,但也支持用户的远程网址?

如果我使用"#{request.protocol}#{request.host_with_port},那将永远适用。我想仅在URL不是远程时才申请。

干杯,

1 个答案:

答案 0 :(得分:0)

假设我正在读你的问题,你可以做点像......

application_helper.rb

def absolute_image_url (url)
  if url[0] == "/"
    return "#{request.protocol}#{request.host_with_port}" + url
  else
    return url
  end
end

或将其添加到模型等中