如何在rails模型中获取完整的资产路径

时间:2012-12-29 11:02:10

标签: ruby-on-rails ruby google-maps gmaps4rails

我在我的应用程序中使用gmap4rails表示要指定标记,您应该在模型中定义方法。但是我怎么能得到完整的图像网址呢?

现在我的模型中有以下代码

def gmaps4rails_marker_picture
{
  "picture" => helpers.asset_path("marker.png"),
   "width" => 20,
   "height" => 33
}
end

def helpers
  ActionController::Base.helpers
end

但这只是给我一张图片的路径?

我也试过这样做

Rails.application.routes.url_helpers.root_url + helpers.image_path("marker.png")

但这给了我一个错误:

Missing host to link to! Please provide the :host parameter, set default_url_options[:host], or set :only_path to true

任何使这项工作的方法?

1 个答案:

答案 0 :(得分:-2)

看看How to set config.action_controller.default_url_options = {:host = '#''} on per environment basis。 您需要设置Rails.application.routes.default_url_options或将:host选项传递给root_url(host:'foo.bar') 要为不同的环境设置不同的主机选项,您可以使用./config/environments/xx.rb文件