3

テキストメールでテーブルを作成しようとしているので、いくつかのヘルパーを書きます:

module MailerHelper
  def field_width(text, width)
    ' ' * (width - text.length) + text
  end

  def cell(text, width)
    output = '| ' + field_width(text, width-2) + " |\n"
    output << '+-' + '-'*(width-2) + '-+'
  end
end

次に、ビューで次のように書きます。

<%= cell 'Test', 10 %>

しかし、私が得たもの(によるとletter_opener)は次のとおりです。

| Test |
+----------+

ご覧のとおり、前に繰り返されているスペースTest。私の質問は、ActionMailer (または私の美しいテーブルを破壊しているもの) がそれを行うのを防ぐ方法です。

メーラーコード:

  def remind(client, invoices)
    @client = client
    @company = @client.company
    @invoices  = invoices.to_a

    days_left = @invoices.first.pay_date - Date.today
    message = @client.group.messages.find_by_period days_left.to_i

    raise 'No messages for this invoices.' if message.nil?

    @template = message.template || if days_left < 0
      t 'message.before'
    elsif days_left > 0
      t 'message.after'
    else
      t 'message.today'
    end

    @text = liquid_parse @template
    @html = markdown_parse @text

    mail(:to => @client.email, :subject => t('message.title'))
  end

  private
    def markdown_parse(text)
      markdown = Redcarpet::Markdown.new Redcarpet::Render::HTML,
        :autolink => true, :space_after_headers => true
      markdown.render text
    end

    def liquid_parse(text)
      renderer = Liquid::Template.parse text
      renderer.render 'company' => @company, 'invoice' => @invoice, 'client' => @client
    end
4

1 に答える 1

1

バグを発見しました。HTML部分にCSSをインライン化するために使用するPremailerが原因でした。

class InlineCSSInterceptor
  def self.delivering_email(message)
    #message.text_part.body = Premailer.new(message.text_part.body.to_s, with_html_string: true).to_plain_text # this is line causing the problem.
    message.html_part.body = Premailer.new(message.html_part.body.to_s, with_html_string: true).to_inline_css
  end
end

Mailer.register_interceptor InlineCSSInterceptor
于 2012-02-25T12:17:44.240 に答える