如何构建静态图像的链接?

时间:2015-09-22 10:06:53

标签: ruby-on-rails ruby-on-rails-4

我意识到当我通过本地postfix服务器在localhost网站上发送邮件(信件)时,它们已成功发送,但每次我需要包含存储在我的徽标中的字母链接:

  

公共/ castle.jpg

应用程序/邮寄者/的 review_mailer .RB:

class ReviewMailer < ApplicationMailer
  default from: 'no-reply@kalinin.ru'

  def welcome_email(review)
    @review = review
    mail(to: 'bla@bla.ru', subject: 'New review create')
  end  
end

视图/ review_mailer /的 welcome_email .html.erb:

<!DOCTYPE html>
<html>
  ...........................
    <p>logo: <%= image_tag 'public/castle.jpg' %></p>
  </body>
</html>

application.rb中:

config.action_mailer.default_url_options = { host: 'localhost' }

development.rb:

  config.action_mailer.raise_delivery_errors = false
  config.action_mailer.delivery_method = :sendmail
  config.action_mailer.default_url_options = { :host => 'localhost:3000' }

reviews_controller:

  def create
    @review = current_user.reviews.build(review_params)
    if @review.save
      ReviewMailer.welcome_email(@review).deliver_now
      redirect_to root_path
  end

创建新评论后,这是控制台输出

ReviewMailer#welcome_email: processed outbound mail in 205.9ms

Sent mail to bla@bla.ru (73.5ms)
Date: Tue, 22 Sep 2015 12:47:11 +0300
From: no-reply@kalinin.ru
To: prozaik81-2@yandex.ru
Message-ID: <5601239fdd0d3_18b43f823a7394382836b@kalinin.mail>
Subject: New review create
Mime-Version: 1.0
Content-Type: multipart/alternative;
 boundary="--==_mimepart_5601239fdbc22_18b43f823a73943828272";
 charset=UTF-8
Content-Transfer-Encoding: 7bit

----==_mimepart_5601239fdbc22_18b43f823a73943828272
Content-Type: text/html;
 charset=UTF-8
Content-Transfer-Encoding: 7bit

<html>
  <body>
    <!DOCTYPE html>
<html>
....................
    <p>logo: <img src="/images/public/castle.jpg" alt="Castle" /></p>
  </body>
</html>
  </body>
</html>

----==_mimepart_5601239fdbc22_18b43f823a73943828272--

Redirected to http://localhost:3000/
Completed 302 Found in 601ms (ActiveRecord: 301.4ms)

邮件(信件)没有被发送,可能是什么错误?

1 个答案:

答案 0 :(得分:1)

我认为你应该在你的环境文件中写这个:

config.action_mailer.asset_host = "http://localhost:3000"

config.asset_host = 'http://localhost:3000'

这将暗示下一个配置:

config.action_controller.asset_host = 'http://localhost:3000'
config.action_mailer.asset_host = 'http://localhost:3000'