Full url for an image-path in Rails 3 Full url for an image-path in Rails 3 ruby ruby

Full url for an image-path in Rails 3


You can also set CarrierWave's asset_host configĀ setting like this:

# config/initializers/carrierwave.rbCarrierWave.configure do |config|  config.storage = :file  config.asset_host = ActionController::Base.asset_hostend

This ^ tells CarrierWave to use your app's config.action_controller.asset_host setting, which can be defined in one of your config/envrionments/[environment].rb files. See here for more info.

Or set it explicitly:

  config.asset_host = 'http://example.com'

Restart your app, and you're good to go - no helper methods required.

* I'm using Rails 3.2 and CarrierWave 0.7.1


try path method

Image.find(:first).image.path

UPD

request.host + Image.find(:first).image.url

and you can wrap it as a helper to DRY it forever

request.protocol + request.host_with_port + Image.find(:first).image.url


Another simple method to use is URI.parse, in your case would be

require 'uri'(URI.parse(root_url) + image.url).to_s

and some examples:

1.9.2p320 :001 > require 'uri' => true 1.9.2p320 :002 > a = "http://asdf.com/hello" => "http://asdf.com/hello" 1.9.2p320 :003 > b = "/world/hello" => "/world/hello" 1.9.2p320 :004 > c = "world" => "world" 1.9.2p320 :005 > d = "http://asdf.com/ccc/bbb" => "http://asdf.com/ccc/bbb" 1.9.2p320 :006 > e = "http://newurl.com" => "http://newurl.com" 1.9.2p320 :007 > (URI.parse(a)+b).to_s => "http://asdf.com/world/hello" 1.9.2p320 :008 > (URI.parse(a)+c).to_s => "http://asdf.com/world" 1.9.2p320 :009 > (URI.parse(a)+d).to_s => "http://asdf.com/ccc/bbb" 1.9.2p320 :010 > (URI.parse(a)+e).to_s => "http://newurl.com"