Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails 3.1: Trouble on displaying images in mailer view files

I am using Ruby on Rails 3.1 and I would like to add my web site logo (that is, an image handled through the new Asset Pipeline) to an e-mail.

If in my mailer view file I state the following:

<% # Note: '@root_url' is my application hostname (eg: http://www.mysite.com) %>
<%= link_to image_tag( "#{@root_url.to_s}/images/logo.png"), @root_url.to_s %>

it doesn't work in production mode (that is, I cannot display the logo image) because I think the Asset Pipeline uses the Fingerprinting technique and in the received e-mail it doesn't. Inspecting the HTML logo element in the e-mail I get something like this:

<img src="http://www.mysitecom/images/logo.png"> # without Fingerprinting

How can I solve the problem?


In my production.rb file I have the following commented out code:

# Enable serving of images, stylesheets, and javascripts from an asset server
# config.action_controller.asset_host = "http://assets.example.com"
like image 362
user502052 Avatar asked Oct 18 '11 15:10

user502052


People also ask

How do you preview mailers in rails?

rails generates a mail preview if you use rails g mailer CustomMailer . You will get a file CustomMailerPreview inside spec/mailers/previews folder. Here you can write your method that will call the mailer and it'll generate a preview.

What is action_ mailer in Rails?

Action Mailer allows you to send emails from your application using a mailer model and views. So, in Rails, emails are used by creating mailers that inherit from ActionMailer::Base and live in app/mailers. Those mailers have associated views that appear alongside controller views in app/views.

Where do you put images in rails?

Images are an integral part of every application, and this is why we are going to learn how to add images to a rails application. To do this, go to app/assets/images and add all your file to the images folder.


2 Answers

in config/environments/production.rb (and other enviroment files needed) add:

config.action_mailer.asset_host = 'http://mysite.com'

after that rails will automatically add hostname in front of paths generated by image_tag

# haml
= image_tag 'foo.jpg'

will become

#html
<img alt="" src="http://mysite.com/assets/foo.jpg" >

...same apply for image_path

#haml
%table#backgroundTable{background: image_path('email-background.jpg'), width: '100%', :border => "0", :cellpadding => "0", :cellspacing => "0"}

will become

<table background="http://mysite.com/assets/email-background.jpg" border="0" cellpadding="0" cellspacing="0" id="backgroundTable" width="100%">

watch out!!!

# this will make your emails display images
config.action_mailer.asset_host = 'http://mysite.com'

is different than

# this wont make your email images work
config.action_controller.asset_host = "http://mysite.com" 
like image 98
equivalent8 Avatar answered Oct 25 '22 02:10

equivalent8


All of these answers are assuming you're using the asset pipeline, but from your example, you're specifying an image in /public/images - this is not part of the asset pipeline, so all the asset_path based answers won't work, and further your initial fingerprinting supposition is incorrect.

If you put an image in /public/images, you want your image tag to have a src of http://yoursite.com/images/the-image.jpeg, no fingerprint, no asset path, nothing - just hard-code it into your view:

<img src="<%=@root_url%>/images/logo.png">

But, you have to actually have the file in that location! If you have your image in /app/assets/images, then you'll need to use image_tag and the asset pipeline as others have answered.

like image 30
Irongaze.com Avatar answered Oct 25 '22 04:10

Irongaze.com