开发者

Getting path of images from the model in Rails

I'm working on a mapping application which uses a module called Gmaps4Rails. Using this gem I define the path to images from within my model and can have it based on some data which image I'm using. It's working fine in development environment but it breaks on production because it can't find the image folder. This is how the code looks like inside the model:

  def gmaps4rails_marker_picture

    {
    "picture" => "/images/oilpum开发者_StackOverflow社区pjack.png",
    "width" => "32",
    "height" => "32"
    }
  end

Is there any easy way of getting the correct path from within the model (I don't want to hardcode it)?

Regards, Johann


If you are using Sprockets for serving images, try this:

def gmaps4rails_marker_picture    
    {
    "picture" => ActionController::Base.helpers.asset_path('oilpumpjack.png'),
    "width" => "32",
    "height" => "32"
    }
end


Give this a go, it might work:

change

"picture" => "/images/oilpumpjack.png"

to this

"picture" => "#{Rails.root}/public/images/oilpumpjack.png"

i was having a similar problem with another gem.hope it helps

edit

In config/environments/production.rb set

config.serve_static_assets = true
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜