在电子邮件中发送附件[徽标图像] - ActionMailer

时间:2016-03-16 16:24:30

标签: ruby-on-rails actionmailer

有人可以告诉我如何使用动作邮件附加徽标图像。我尝试了以下但我得到一个错误

我收到错误:

undefined local variable or method `attachments' for #<#<Class:0x007f84d2282400>:0x007f85714656d8>
mailers/mailer_report.rb

-

class MailerReport < ActionMailer::Base

  def report_advert(report)
    attachments.inline['img-logo-main.png'] = File.read('/images/img-logo-main.png')
    @report = report
    mail( to: "akunorbea@gmail.com",
          subject: "<Report> #{report.category_report.name} | #{report.advert.title}", 
          from: "<no-reply@africanjober.com>")
  end
end

视图/ mails_report / report_advert.html.erb

      <div style="color:#0000FF">Reported Advert: <%= @report.advert.title %></div>
        <%= image_tag attachments['img-logo-main.png'].url -%>
        Report Catgeory: <%= @report.category_report.name %><br/>
        Advert Catgeory: <%= @report.advert.category_advert.name %> / <%= @report.advert.category_advertskill.name %><br/>
        Advert link: <%= link_to "#{@report.advert.title}", userr_advert_url(@report.advert.userr, @report.advert) %>
      </div>

report_controller.rb

class ReportsController < ApplicationController
  respond_to :html, :xml, :json
  before_action :set_report, only: [:show, :edit, :update, :destroy]

  def create
    @report = Report.new(report_params)
    @report.save
    respond_to do |format|
      if @report.save
        MailerReport.report_advert(@report).deliver
        format.html { redirect_to(search_adverts_path, notice: 'Report was successfully sent.') }
        format.json { render json: @report, status: :created, location: @report }
      else
        flash.now[:error] = 'Cannot send report.'
        render :new
      end
    end
  end
end

1 个答案:

答案 0 :(得分:0)

尝试:File.read('app/images/img-logo-main.png')