Full Image Path URL in Rails 3

I have an image that contains a carrier load:

Image.find(:first).image.url #=> "/uploads/image/4d90/display_foo.jpg" 

In my opinion, I want to find an absolute URL for this. Adding root_url leads to a double / .

 root_url + image.url #=> http://localhost:3000//uploads/image/4d90/display_foo.jpg 

I can’t use url_for (what I know) because it either allows passing a path or a list of parameters to identify the resource and :only_path option. Since I do not have a resource that can be identified through the "controller" + "action", I can not use the parameter :only_path .

 url_for(image.url, :only_path => true) #=> wrong amount of parameters, 2 for 1 

What would be the cleanest and best way to create a path to the full URL in Rails3?

+34
url ruby ruby-on-rails path ruby-on-rails-3
Mar 30 '11 at 10:00
source share
9 answers

try the path method

 Image.find(:first).image.path 

UPD

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

and you can wrap him as an assistant so that dry forever

 request.protocol + request.host_with_port + Image.find(:first).image.url 
+33
Mar 30 '11 at 10:01
source share

You can also set the CarrierWave asset_host configuration asset_host as follows:

 # config/initializers/carrierwave.rb CarrierWave.configure do |config| config.storage = :file config.asset_host = ActionController::Base.asset_host end 

This ^ tells CarrierWave to use your config.action_controller.asset_host application, which can be defined in one of your config/envrionments/[environment].rb . See here for more information.

Or install it explicitly:

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

Restart the application and you are good to go - no auxiliary methods are required.

* I am using Rails 3.2 and CarrierWave 0.7.1

+90
Feb 21 '13 at 20:40
source share

Another easy way to use is URI.parse, in your case it will 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" 
+9
May 9 '12 at 3:21
source share

Just answer the floor and provide an assistant:

 # Use with the same arguments as image_tag. Returns the same, except including # a full path in the src URL. Useful for templates that will be rendered into # emails etc. def absolute_image_tag(*args) raw(image_tag(*args).sub /src="(.*?)"/, "src=\"#{request.protocol}#{request.host_with_port}" + '\1"') end 
+5
Aug 18 '11 at 16:38
source share

There are quite a few answers here. However, I did not like any of them, since they all rely on me to remember to explicitly add a port, protocol, etc. I believe this is the most elegant way to do this:

 full_url = URI( root_url ) full_url.path = Image.first.image.url # Or maybe you want a link to some asset, like I did: # full_url.path = image_path("whatevar.jpg") full_url.to_s 

And the best thing about this is that we can easily change only one thing, and no matter what it may be, you always do it the same way. Tell me if you want to abandon the protocol and use the protocol -related URL , do this before the final conversion to a string.

 full_url.scheme = nil 

Yay, now I have a way to convert the resource image URLs to relative protocol URLs that I can use in a piece of code that others might want to add to their site, and they will work regardless of the protocol they use on their site (provided that your site supports any protocol).

+2
Nov 18 '13 at 14:10
source share

You cannot reference the request object in the email, as for:

 def image_url(*args) raw(image_tag(*args).sub /src="(.*?)"/, "src=\"//#{ActionMailer::Base.default_url_options[:protocol]}#{ActionMailer::Base.default_url_options[:host]}" + '\1"') end 
+1
Jun 29 '12 at 22:19
source share

In fact, you can easily do this with

 root_url[0..-2] + image.url 

I agree that this does not look too good, but it does its job .. :)

+1
Jul 03 '15 at 11:14
source share

I used default_url_options because request not available in the mailer and avoided duplicating the host name in config.action_controller.asset_host if it has not already specified it.

 config.asset_host = ActionDispatch::Http::URL.url_for(ActionMailer::Base.default_url_options) 
+1
Sep 08 '15 at 16:43
source share

I found this trick to avoid a double slash:

 URI.join(root_url, image.url) 
0
Nov 21 '17 at 19:20
source share



All Articles