paul.ago
paul.ago

Reputation: 4103

Rails 4: get an image full url generated by Sprockets in production environment (inside rake task)

Trying to get the image full URL inside a view rendered by a mailer.

Is there a proper way in Rails 3+ to get the full url generated by Sprockets?

I know of the request object hack, but since the mailer is invoked inside a rake task, the request data is not available, obviously.

I'm running Rails 4 beta1 (edge)

Upvotes: 3

Views: 1340

Answers (1)

Chris Aitchison
Chris Aitchison

Reputation: 4654

In Rails 4 you need to have the following in your production.rb

config.action_controller.asset_host = 'yourdomain.com'
config.action_mailer.asset_host = 'http://yourdomain.com'

Having one with the protocol (http) and one without is deliberate. Maybe it will be different before Rails 4 comes out of beta, but if you have the protocol in action_controller.asset_host then you get urls like http://http://yourdomain.com, and if you don't have the protocol in action_mailer.asset_host it is not recognised as a valid URL because it doesn't validate correctly against an internal Rails Regex.

Then you can use the following in your mailer template:

<%= image_url('mail/awesome.gif', only_path: false) %>

Upvotes: 4

Related Questions