Rails 3.1 Absolute Url to an Image

Rails 3.1 absolute URL to an image

See the Using asset hosts section in the documentation. You need to specify an asset_host. You can also construct it dynamically from the request chaining "#{request.protocol}#{request.host_with_port}"

How do I get an absolute URL for an asset in Rails 3.1?

It would appear that as of recently, sass-rails now interprets the image_url command within a scss file in the expected manner, resolving to the final location of the image in question.

Full url for an image-path in Rails 3

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

How do I get full URL to an image in a Rails asynchronous mailer?

Well this is odd behaviour, but I've resolved this issue. It turns out that unless the action_mailer.asset_host starts with http:// then it will be ignored. There is a Regex in actionpack/lib/action_view/helpers/asset_url_helper.rb that defines a valid ActionMailer URI:

URI_REGEXP = %r{^[-a-z]+://|^(?:cid|data):|^//}

However, if you put a http:// in front of the action_controller.asset_host then you will end up with links to http://http://myfullappurl.dev

So to resolve it I had to add the following to my development.rb

 config.action_controller.asset_host = 'myfullappurl.dev'
config.action_mailer.asset_host = 'http://myfullappurl.dev'

Just to be clear, this is Rails 4.0.0beta1 with the emails being sent asynchronously with Sidekiq, I am not sure if this affects Rails 3.

Get Full URL for ActiveStorage image

I'm not sure if this is the recommended way, but you can use the polymorphic_url helper:

polymorphic_url object.image.variant(resize: '140x140')

How to have asset_path return an absolute URL?

Use asset_url

<meta property="og:image" content="<%=asset_url('logo_400x200.jpg') %>" />

Generate absolute URL from an array of route components

You can simply use url_for:

link_to 'My account', url_for([:edit, :admin, :account, :details])

Check out this document



Related Topics



Leave a reply



Submit