Full Url for an Image-Path in Rails 3

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

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.

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')

Rails 3: How to get image path in Controller?


ActionController::Base.helpers.asset_path('missing_file.jpg')

Access Asset Path from Rails Controller

Full URL with url_for in Rails

Use the :host option. For example, you can use:

url_for(@book, :host => "domain.com")

Note: with Rails 3 and above, use polymorphic_url instead of url_for.

Rails 3.1 asset urls in SCSS files do not seem to be referencing the assets correctly

I have tried various solutions. The most elegant one was the following:

.foo {
background-image: url('foo.png')
}

which automatically converted to url('/assets/foo.png') by the SCSS compiler.

How can I use image_path inside Rails 3 Controller

You can use view_context in your controller when doing 'view' tasks like generating links. The good thing about it is you don't have to include the view helpers in your controller.

e.g. in your controller you can create a variable which will be a html link with link_to or url_for if you just want the link, 'only_path' option set to false should give you absolute url.

link = view_context.url_for(:action => 'login', :controller => 'members', :only_path => false)

Hope this helps.



Related Topics



Leave a reply



Submit